Skip to content

feat(#1729): session-level dual-stream shape observer with one-shot conflict WARN - #1837

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-10-01_stream-shape-watch
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-10-01_stream-shape-watch

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Part of #1729 — completes the observer scope remaining after #1835

#1729's deadlock had two missing layers. #1835 ships the traffic gate for the identified producer (dsh compaction calls refused at the proxy). This PR ships the general observer the issue scoped as the remaining value: session-level detection of structurally exclusive request shapes sharing one session id, for unclassified personas (autoreview / subagent / future replay variants).

What

New src/stream-watch.ts + one call per protocol prepare site (4 sites, post-processTurn, src/server.ts:3387/3620/3869/4162). Each pipeline request is profiled by normalized message count vs token magnitude relative to that request's own model window:

Once BOTH classes have each been seen ≥2 times on one session id → exactly one WARN per session, fingerprinting both shapes (latest msgs/tokens/usage% + truncated nudge verdict) and pointing at #1206/#394/#1835/#1729. The same fingerprint lands in the #1206 conflict ledger (dual-stream) → visible in acp_status, the web banner, and /__bili/stats with zero new UI plumbing.

Why this layer, why these thresholds

The kernel's per-request nudge gate is correct design and stays untouched (kernel contract: never relax gates for one malformed payload). The missing piece was observation: when two producers share a session id, no single request can satisfy pendingT1≥50000 ∧ growth≥floor ∧ payload-compressible, usage pins near the ceiling, and today nothing aggregates that pattern. The thresholds are internal diagnostic constants (precedent: EMERGENCY_NUDGE_ESCALATION_PCT in src/server/budget.ts), window-relative so they scale from a 167K window to a 1M window without retuning. State is O(1) in session.metadata.streamWatch — round-trips through existing wholesale metadata persistence, the one-shot flag survives restarts, old versions ignore the unknown key, corrupt state degrades to fresh.

Relationship to the other layers:

Behavior change disclosure (additive only)

  • One new one-shot WARN line per session, emitted only when both shapes alternate periodically. No change to any existing log line.
  • One new ConflictKind value (dual-stream). All consumers verified generic (no exhaustive switches); ledger ring cap unchanged.
  • One new metadata key (streamWatch). Persistence format/version unchanged.
  • Nudge gate semantics, wire format, message shapes, retry/cache behavior: unchanged.

Config surface

None. No new config field, env var, or CLI flag. Thresholds are internal constants per the precedent above; if tuning ever proves necessary that is a separate owner-gated discussion.

Honest boundaries

Detects the observed signature (giant single-message ↔ multi-turn). Mid-shape variants (e.g. a 5–10-message replay call) classify as multi-turn and blend into the main flow today — threshold evolution would be needed if triage surfaces such a variant. Concurrent same-shape sharing across endpoints is a different signature and out of scope here.

Pre-flight

  • npm run typecheck clean
  • Targeted tests: tests/stream-watch.test.ts (new, 6 cases: classifier boundary table incl. exact-half and 1M-window scaling, one-shot trigger sequence, non-trigger sequences, restart round-trip, corrupt-state tolerance, reason truncation) + tests/issue1206-thirdparty-scan.test.ts (conflict-watch union consumer) = 26/26 pass
  • npm run build OK, module bundled into dist/index.js
  • Full suite + e2e left to CI (diff touches src/server.ts → ci-e2e auto-runs)

@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-01_stream-shape-watch (e2e1acc)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1821-pr.1837.7ca4919

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-pr1837.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] 🏷 Review 完成(验证模式:不重做工作,核对代码与声明、本地重跑定向验证、看 CI)。结论:可合(CI 全绿 + rebase 干净 + 声明全部核实)。

① 审原 issue(方向关)

#1729 是真实死锁:同一 session id 上交替出现两种结构互斥的请求流(多轮 loop ~107 msgs/46K ↔ 巨型单消息 1–2 msgs/~165K),没有任何单个请求能同时满足 nudge 三门(T1 pending≥50K ∧ growth≥floor ∧ 当前载荷可压缩)→ usage 贴顶 ~15 分钟无压缩且全链路无告警。issue floor 2 走完三步审核后给出设计规格(纯分类器 + O(1) 会话状态 + 一次性 WARN + ledger 落账 + 零新端点);你 floor 4 实锤辅流身份(dsh-compaction-basic summarizeWithLlm)并明确保留「观察器部分(未知 persona 的形状跳变告警)仍开放」。本 PR 逐条实现的就是该设计,方向正确、scope 与 #1729 剩余价值吻合、无漂移。

② 回归审查(触碰的既有行为清单)

diff 共 4 文件(+296/-1),逐项判定:

既有行为 结论
kernel decideNudge / nudge gate 语义 未动,per-request 语义不变(显式保留 kernel 契约)
wire 协议 / 消息形状 / SSE 未动——纯 log + metadata 增量,转发 body 字节不变
conflict ledger ring cap(CONFLICT_LEDGER_MAX=20) 不变;ConflictKind 联合加一个成员,已核所有消费者(conflictEventsOf 只做形状校验、formatConflictSection 泛型渲染、summarizeConflicts 计数、web banner kinds 列表)无一处穷举 switch,旧数据向后兼容
会话持久化格式/版本 不变——metadata 本就 wholesale round-trip(persist.ts:682 spread / :760 ?? {});旧版本忽略新 key streamWatch;损坏状态经防御读取(num()/sightingOf())降级为 fresh,不崩
passthrough 请求(#1117 mark / 全局 passthrough) 不进管线、不进 observer——与诊断 scope 一致
prepareResponsesCompact 快路径(bili-forged compaction item 剥离) 不跑 processTurn,天然排除——bili 自己的流量不是第二 producer,排除正确
side request(#388) 绕开管线,不进 observer
缓存/重试/增量游标/去重键 不涉及——只读观测
热路径异常面 observeSessionStream 纯同步计算、全输入走类型守卫,无新增可抛面;markDirty→scheduleSave 与每 turn 既有保存同路

高危模式逐项过:无语义漂移、无 fail-fast 变吞错、无阈值/时序/输出格式漂移(唯一新增输出:满足双流条件时的一条 warn 行 + 一条可选 ledger 事件)。

③ 代码审查(主关)

④ 测试有效性

新增 6 case(tests/stream-watch.test.ts)钉住行为而非 mock:分类边界表(恰好半窗 vs 半窗−1、1M 缩放 500000/499999、window=0)、one-shot 触发序列 A,B,A→不告 / B→恰 1 告(断言 level=warn、前缀、指纹字段、ledger kind、summarizeConflicts 计数、后续交替不再重复)、非触发序列(A,A,B / B,B / 侧调+A+A → 0 告 0 账)、重启 JSON round-trip 保持 warned、损坏状态容错、reason 截断 120。若阈值被改或 one-shot 语义被破坏,这些 case 立即抓得住。

⑤ diff 整洁度

4 文件全部与本 PR 目的相关:union 加 1 行、4 处各加 1 行调用、2 个新文件(模块 + 测试)。无空白重排、无生成物、无 lockfile 变更。干净。

⑥ 验证证据

  • rebase 到最新 master(c0fd62e4;PR base e669287d 落后 3 commit,即 fix(dsh): retry failed model-window resolves — one boot race no longer strips x-bili-plugin-context-window #1836,只碰 src/agent/dsh-native.ts + CONFIGURATION en/zh,与本 PR 文件零重叠)→ rebase clean。
  • rebased head 上:typecheck clean;定向测试 tests/stream-watch.test.ts + tests/issue1206-thirdparty-scan.test.ts 26/26 通过;build OK,dist/index.js 已含模块符号(dual-stream ×2 / streamWatch ×4 核验)。
  • CI @ head e2e1acc3:13/13 success——单测全矩阵(ubuntu+windows × node 22/24)、e2e(real codex)、hermetic registry e2e、real codex/pi/dsh/opencode lanes(zero-token fake upstream)、build-artifact、version-guard、codex overlay MCP。PR mergeable=True(behind master,不构成障碍)。

结构化七问

  1. 是什么问题:同一 session id 上交替出现两种结构互斥的请求流时,nudge 三门死锁、usage 贴顶不压缩,而全链路没有任何一处能指出「两个 producer 在共享一个会话」。
  2. 什么原因导致的:kernel per-request nudge gate 本身设计正确(不应为畸形载荷放宽);缺的是 bili 侧会话级流量观测——自动识别其他压缩插件存在并在任何可能的情况下报告警告 #1206 [conflict] 只静态扫插件目录,EMERGENCY/OVER-LIMIT 日志逐条打印无人聚合。
  3. 怎么修复的:会话级形状观察器(纯分类器 + O(1) metadata 状态)挂四个 prepare* 管线,两类各见 ≥2 次 → 每 session 一次性 WARN(带双流指纹)+ conflict ledger 一条 dual-stream,acp_status/web banner/stats 直接可见,零新端点、零新配置。
  4. 是否有回归问题:无。nudge gate/wire/持久化版本/缓存策略均未触碰;ledger kind 纯增值且所有消费者已核 generic;热路径无新增异常面。
  5. 是否破坏缓存:不涉及——只读观测 + 本地日志,不触碰任何失效策略。
  6. 是否改了设计方向:没有。显式保留 kernel per-request 契约,加正交观测层;与 feat(dsh): ship in-package bili preset as the roster default (#1772) [discussion draft] #1827/dsh.bundle.patch.yml 禁用自动压缩在 web profile 下静默失效 #1772(源头不试)、fix(#1729): refuse dsh native compaction calls at the proxy gate #1835(流量拦已知 persona)叠成三层互为兜底。
  7. 是否引入了新的配置:没有。阈值为内部诊断常量(先例 EMERGENCY_NUDGE_ESCALATION_PCT),无新 env/config/CLI surface。

合并判定:可合。 合并按钮留给你(系统规则禁止 agent 合并):#1837

@ranxianglei ranxianglei closed this Oct 1, 2026
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