Skip to content

feat: #2359 intercept azure Responses WebSocket lanes; strip consumed x-bili-* at azure egress - #2371

Merged
ranxianglei merged 1 commit into
masterfrom
2026-10-07_azure-responses-ws
Oct 8, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-10-07_azure-responses-ws

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Intercepts OpenCode V2 azure Responses WebSocket lanes (previously bypassed) and keeps them under Azure's WS custom-header cap so the connection actually succeeds.

What changed

  • src/agent/opencode-v2.ts — the experimental.ws.handshake guard now admits providerID azure alongside openai, keeping the existing ws:// scheme + /responses pathname checks. Any other provider stays unintercepted.
  • src/responses-ws.ts — at final upstream egress, bili-internal x-bili-* headers are stripped only for hosts ending in .openai.azure.com. Extracted into exported finalizeResponsesWsUpstreamHeaders(url, headers) (used by ResponsesWsUpstream.connect) so the exact wire decision is unit-testable.

Behavior changes (old → new)

  1. Azure WS interception. Old: an azure providerID /responses WS socket was never rewritten/stamped → bypassed bili entirely (no compression, not counted, stale /acp). New: intercepted and compressed like openai. Why: OpenCode V2 Azure Responses WebSocket bypasses native ACP interception; status can show stale compressed context #2359 — cover azure on the same lane.
  2. Azure egress headers. Old: every non-hop client header (incl. consumed x-bili-*) was forwarded to the azure upstream; a typical OpenCode handshake (6 OpenCode x-* + 5 x-bili-* + api-key = 12) exceeded Azure's documented 10-custom-header cap → gateway closed with 1011 before any event. New: x-bili-* dropped at egress to *.openai.azure.com only, landing at ~7 custom headers. Auth (api-key) + OpenCode headers preserved; every non-azure host is byte-for-byte unchanged.

Both compression modes

The change only governs which model traffic reaches the upstream and what headers ride it; it does not touch the acp_summary carrier, tool/nudge injection, or preflight/compact paths. Plugin-mode and proxy-mode summary carriers are unaffected.

Config surface

None added. The .openai.azure.com match is hardcoded to match the verified hotfix scope (Azure OpenAI endpoints are uniformly *.openai.azure.com); a config knob would invent surface without need. Flag if you'd rather make the strip host(s) configurable.

Tests

  • New pure unit test of finalizeResponsesWsUpstreamHeaders: azure strips x-bili-*, retains auth + OpenCode headers, always drops hop/sec-websocket markers; non-azure retains x-bili-*. Asserts the exact wire set directly — stronger than a permissive round-trip mock (which would miss the real gateway rejection).
  • New guard test: azure is intercepted (URL rewritten + stamped); an uncovered provider is left untouched.
  • Control assertion in the existing full-flow test: a non-azure upstream still receives x-bili-* verbatim.
  • Local gates green on the rebased head: typecheck clean, tests/responses-websocket.test.ts 40/40, build OK. Full regression left to CI.

Note: full recovery/error-code parity and compression folds specifically over the Azure WS transport remain unverified hermetically (a live-azure limit-enforcing mock isn't reachable without /etc/hosts); the unit-level wire assertion covers the header-leak regression and CI covers the rest.

Closes #2359

… x-bili-* at azure egress

OpenCode V2's experimental.ws.handshake hook only admitted providerID
"openai", so an azure providerID socket was never URL-rewritten/header-stamped
and bypassed bili entirely (no compression, stale /acp carriage). Two coupled
changes:

- Admit "azure" alongside "openai" in the guard (src/agent/opencode-v2.ts),
  keeping the existing ws:// scheme + /responses pathname checks. Any other
  provider stays unintercepted.
- Strip bili-internal x-bili-* headers at final upstream egress ONLY to hosts
  ending in .openai.azure.com (src/responses-ws.ts). Azure OpenAI caps custom
  request headers on the WS upgrade (documented 10); the forwarded x-bili-*
  metadata (already consumed at ingress) plus OpenCode's own x-* headers push a
  typical handshake past it, so the gateway closes with 1011 before any event.
  Auth + OpenCode headers stay intact; every other host keeps its existing
  forwarding byte-for-byte. Extracted into exported finalizeResponsesWsUpstreamHeaders
  so the exact wire decision is asserted directly rather than via a permissive
  round-trip mock.

Tests: pure unit test of the egress decision (azure strips x-bili-*, non-azure
retains them, hop markers always dropped); guard test proving azure is
intercepted while an uncovered provider is left untouched; control assertion in
the existing full-flow test that a non-azure upstream still receives x-bili-*.

Closes #2359
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-07_azure-responses-ws (db643eb)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2543-pr.2371.2d36030

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

Option B — Download artifact

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

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 已拉取并审阅分支 2026-10-07_azure-responses-ws(head db643eb,单提交,rebase 到当前 master)。3 文件、85+/4-,单一目的,diff 干净。结论:可合并,待 4 个仍在跑的全量回归 test job(windows/ubuntu 22/24)落地为绿——其最直接相关的兄弟 lane「OpenCode V2 Responses WebSocket ACP」已绿。

结构化七问

  1. 是什么问题:OpenCode V2 走 Azure 的 Responses WebSocket(providerID="azure")时,bili 原生拦截钩子只认 openai,azure 在 URL 重写/打戳前就 return → 整条流量绕过 bili(不压缩、不计入、/acp 过期)。即 OpenCode V2 Azure Responses WebSocket bypasses native ACP interception; status can show stale compressed context #2359。

  2. 什么原因导致的:根因是 experimental.ws.handshake 守卫把 provider 白名单写死成 openai 一家(与 OpenCode V2.0.20 OpenAI default WebSocket bypasses bili native HTTP interception; only title request creates empty ACP session #1844/feat: ACP-aware OpenCode V2 Responses WebSocket proxy + generic WS bridge shell (#1844/#1467) #1968 同源);azure 说同一套 OpenAI Responses WS 词汇却没被放行。决策点上的遗漏,非表面现象。

  3. 怎么修复的:两层。(a) 守卫 !=="openai" 放宽为 !("openai" && !"azure"),scheme + /responses pathname 检查原样保留,其他 provider 仍不拦截——在决策点修根因,不做事后过滤。(b) 放行 azure 后暴露第二机制:典型握手(6 OpenCode x-* + 5 x-bili-* + api-key=12)超 Azure WS 升级自定义头上限,网关在任何事件前以 1011 关闭。故把最终出站头抽成导出函数 finalizeResponsesWsUpstreamHeaders(url, headers)(connect() 唯一调用点):先照旧去 hop-by-hop + sec-websocket-*,再仅对 *.openai.azure.com 去掉已消费的 x-bili-*;api-key 与 OpenCode 头保留。

  4. 是否回归问题:无(逐项过既有行为)——

    • openai WS 拦截:守卫只是 OR 进 azure,openai 分支字节不变;
    • 非 openai/非 azure provider:仍提前 return(guard 测试用 anthropic 验证未被动);
    • 非 azure 上游出站头逐字节不变:新函数对非 azure 只做与旧内联逻辑完全相同的 hop+sec-websocket 剥离,azure 条件是额外叠加;full-flow 控制断言钉住「非 azure 仍原样收到 x-bili-*」;
    • 响应头转发路径(responses-ws.ts:502 handshakeHeaders,上游→客户端方向)未触碰;
    • 两模式 acp_summary 载体 / tool·nudge 注入 / preflight·compact 均未触及;
    • 高危模式逐项:无 fail-fast 变吞错、无阈值/时序/输出格式漂移、无默认值变更。唯一行为变化有意且已披露(azure 开始被拦截;azure 出站丢 x-bili-*)。
  5. 是否破坏缓存:不涉及。会话身份在入站就已捕获(egress 之前),出站丢 x-bili-* 不影响身份解析与前缀缓存;既有 openai/其他会话零变化。

  6. 是否改设计方向:不涉及架构/边界/数据流转向。只是把 azure 纳入既有 WS lane + 一个作用域受限的出站过滤器。.openai.azure.com 匹配带前导点号,正确排除 *openai.azure.com 冒名;硬编码可接受,符合配置面纪律(作者已 flag 若要可配 host 再说,我同意暂不加)。

  7. 是否引入新配置:不涉及。无新 env/config/CLI。

测试有效性:有牙。纯函数单测直接断言精确 wire 集合(正例留 auth+OpenCode、负例去 hop/sec-websocket/x-bili;非 azure 留 x-bili)而非宽松 round-trip mock;guard 集成测证 azure 被拦截(URL 重写+打戳)、未覆盖 provider 不动;full-flow 加控制断言。这是能抓住真实网关拒绝的最小断言。

诚实边界(作者已声明,我认同):无法 hermetic 复现真 azure 的上限强制(mock 需 /etc/hosts),故 1011 本身未在 e2e 复现,只有精确头集被钉住。我认可——「在出站丢弃已消费的 bili 内部头」无论上限具体是多少都恒安全且是卫生行为,且严格限定 azure、跨主机零回归,该数字不影响判定。(我从公开文档未能独立确认 Azure「恰好 10 个自定义头」的具体数值,但它不是本 PR 正确性的门槛。)

顺带观察(非本 PR 缺陷,不单独立项):x-bili-* 元数据目前仍随 WS 出站转发到非 azure 上游(如 api.openai.com)——这是既有行为、跨所有 lane 存在,属低危信息卫生项(暴露 bili 在用 + 会话 id),且「是否对所有上游都剥离内部头」是产品判断而非明确 bug。按反碎片/不制造噪音原则我先记在此处,不擅自立项;owner 若希望追踪我再补一条带来源标记的 issue。

判定:可合并,待上述 4 个全量回归 test job 转绿(最相关 lane 已绿)。CI 落地后我回帖确认。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 CI 全绿:db643eb 上 24/24 check-runs 全部 success、0 pending——含最后收尾的 4 个全量回归 test job(windows/ubuntu 22/24)、hermetic registry e2e、以及最相关的「OpenCode V2 Responses WebSocket ACP」lane。

上一条七问结论不变:可合并。唯一待办(CI 转绿)已满足。合并动作仍归 human:#2371

@ranxianglei
ranxianglei merged commit 3234e5a into master Oct 8, 2026
24 checks passed
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.130-master.3234e5a (rolling master tag).

npm install -g billion-context@0.0.130-master.3234e5a  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

@ranxianglei
ranxianglei deleted the 2026-10-07_azure-responses-ws 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.

OpenCode V2 Azure Responses WebSocket bypasses native ACP interception; status can show stale compressed context

1 participant