Skip to content

[PR] feat: dsh client Context tab with accurate post-compression stats (#3862) - #3886

Merged
ranxianglei merged 7 commits into
masterfrom
2026-10-20_context-view-panel
Oct 20, 2026
Merged

ranxianglei merged 7 commits into
masterfrom
2026-10-20_context-view-panel

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #3862

What

A native Context tab in the dsh client (per-session, next to Chat/Trajectory) that shows billion-context's own post-compression view, plus the machine-readable data behind it: a structured contextView field on GET /__bili/plugin/status.

Owner directive (#3862): reference dsh-context's basic functionality, guarantee the data is accurate, integrate part of dsh's native UI code — and (follow-up comment) dig a small hole in the official client instead of bringing react/iframes into bili. So the UI runs ONLY inside dsh's own client runtime (react resolved by dsh, same pattern as every other dsh client plugin); bili contributes a small tsup entry (dist/agent/dsh-native-client.js, react stays external) and one JSON field on a route it already serves.

Why this fixes #3862: dsh-context computes its bar from DSH's local event stream, where bili's folds never appear (storage: shadow), so its segments stay pre-compression forever (the #3303 diagnosis). Here the numbers come from the proxy that PERFORMED the fold: the same live recompute that drives the existing panel breakdown, published verbatim. Post-compression, the tab shows the smaller number — by construction.

Surfaces

  1. src/context-view.ts (new) — buildContextView({nudge, systemPromptTokens, limit, session}):
    • categories in fixed order [sysprompt, tool, text, code, summaries, system] (zero rows omitted); sentTotal = classified + systemPromptTokens; pct rounded — mirrors kernel/src/panel/panel.ts math exactly, so bar and panel can never disagree;
    • anchor = displayContextBest (usage > usage-anchored estimate > upper bound), carried with its OWN scale label — never mixed with the estimate-scale bar (release v0.1.6 #18/[Bug] host-plugin notice 文案过宽:实测 dsh-context 头部与 contextTokens 相符(14 ms 内),只有分段构成条是压缩前口径 #3303 discipline);
    • folds trail (newest 20: folded originals S, summary sigma, reclaimed, saved-so-far, paid-back flag via kernel fold economics) and switches trail (model/key/prompt/restart events with attributed missed tokens) from the existing cache ledger — additive read, no ledger changes beyond exporting two types.
  2. src/plugin.ts / src/server/admin.ts — contextView added to the /__bili/plugin/status JSON (null until the session's first request; built from the nudge recompute the handler already performs — zero extra processTurn calls). The route now echoes access-control-allow-origin back for origins the existing admin gate already trusts (loopback names regardless of port, configured trusted hostnames) so a cross-origin browser reader (the dsh client page) can fetch it. The gate itself is untouched: untrusted origins still get 403 before dispatch; the echo never widens access.
  3. src/agent/dsh-native-client.ts — new deferred mount on the dsh conversation.view slot (verified present in dsh 0.1.1-rc.2; built-ins register chat@0/trajectory@10, so order 30 sits right of any installed Context tab): header sent/limit + billed anchor chip, horizontal stacked bar (deterministic palette), legend, merged fold/switch event list, 10 s poll with swap-not-stack refresh and honest degraded states (waiting / stale build / no data / no proxy). Reuses the file's existing origin-discovery machinery; the Web-UI opener button is now a single module-level builder shared with the settings embed. No package.json change needed (deferred slots.inject fails open where the face doesn't exist).
  4. CLIENTS.md — paragraph documenting the tab and its two scales.

Behavior changes (explicit)

Surface Old New Why
GET /__bili/plugin/status JSON no contextView additive contextView field (null pre-first-request) the data source for the tab; all existing consumers (pi/opencode/kimi/zcode shared status readers) unaffected — nothing removed or renamed
Same route, HTTP headers no ACAO echo echoes Origin when it passes the EXISTING admin gate cross-origin browser read for the dsh client; access set unchanged (gate runs first, 403 otherwise)
dsh client UI settings-section embed only additional per-session Context tab (dsh ≥ rc2) the feature; settings/plugins embeds byte-identical behavior
Everything else unchanged unchanged —

Verification

  • npm run typecheck clean on rebased head (master @ 39fb841).
  • Targeted suites green locally: tests/issue3862-context-view.test.ts (new: unit math incl. exact category-sum invariant, anchor ladder, seeded-ledger fold/switch mapping; integration through real server: additive regression, CORS echo trusted-only, 403 untrusted), tests/dsh-settings-ui.test.ts (tab render/poll/degraded states + existing embed pins), tests/dsh-acp.test.ts, tests/anthropic-pck-identity.test.ts, tests/update-notes.test.ts, tests/advisory-panel.test.ts, tests/issue404-restore-timestamps.test.ts, tests/issue2029-status-baseline-provenance.test.ts — 50/50 pass. Full suite delegated to CI per local-testing discipline.
  • npm run build clean; dist/agent/dsh-native-client.js checked: react external, loader id billion-context intact, tab mount + status path present.

Config surface: none (no new config fields, env vars, or CLI flags).
kernel-change: none (kernel consumed read-only; fold economics come from its existing report).

#3862)

Host-side meters (dsh-context and kin) anchor on dsh's local event stream,
which never sees bili's folds, so their bars stay on the pre-compression
caliber forever (dsh-context#173). bili knows the exact post-fold sent
bytes, so it publishes them two ways:

- GET /__bili/plugin/status gains an additive contextView field: the
  machine-readable twin of the /acp panel breakdown (sentTotal vs limit,
  category segments mirroring buildStatusPanel math exactly, the
  usage-grade anchor from displayContextBest on its own labeled scale,
  fold/switch trails mapped from the cache ledger). The route echoes
  access-control-allow-origin for origins the existing trusted-origin gate
  already admitted, so a cross-origin browser reader can consume it; the
  gate itself is untouched, no access widening.
- The dsh native plugin registers a session-scoped Context tab
  (conversation.view slot, id bili-context) rendering that view with plain
  React from dsh's own runtime (no iframe, no new dependencies): stacked
  bar, legend, events list, 10s poll, honest degraded/stale/waiting states,
  open-Web-UI escape hatch.
@ranxianglei ranxianglei changed the title feat: dsh client Context tab with accurate post-compression stats (#3862) [PR] feat: dsh client Context tab with accurate post-compression stats (#3862) Oct 20, 2026
@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-20_context-view-panel (7f91858)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.4611-pr.3886.28e14fc

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-pr3886.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

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

查重: 不重复。本 PR 是实现 #3862(dsh 客户端 Context 标签页 + /__bili/plugin/status 的 contextView 字段)的唯一在途 PR,无并列实现。CI 红门方面:master 自 2026-10-19 起 docs-sync/unused-exports 两个静态门禁本就已红(#3870、#3878 已立项,#3871/#3887 两个修复 PR 在途)——本 PR 不重复修那部分,只修自己引入的导出问题(见下)。

标签: dsh + scope/ui + p3(新增功能,dsh 客户端界面域,非缺陷故不打 bug;p3 = 功能类无用户损害)。

当前状态(初审):

The five ContextView* shapes and ModelSwitchStats are consumed structurally (buildContextView's return value / cache-ledger internals); nothing outside their own files names them, so the #1440 P6 gate flags them as INTERNAL_ONLY. Drop the leading export keyword only - no behavior change. The remaining local gate hit (src/log-mask.ts maskUrlForPublish) is pre-existing master drift tracked by #3870 with fixes in flight (#3871/#3887); deliberately not bundled here.
awork added 2 commits October 20, 2026 08:42
The #1357 chain-advisory response carries a panel string exactly like the stale-running-build case, so the Context tab's degraded ladder misdiagnosed it as 'the running proxy predates this install — restart fixes it' when the real state is simply 'no session exists yet for this conversation'. The phase check routes it to the neutral not-yet notice; adds the negative test row pinning both directions.
Stray leading space before the closing brace of an unrelated test (#3100 embed pre-flight, tests/dsh-settings-ui.test.ts) and in CLIENTS.md (trailing-space blank line plus a leading space before the Kimi Code heading) — none of it related to the feature; keeps every diff line on-purpose.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

Review 完成,已在分支上补了 2 个修复 commit 并推送(a90d59a77..a3024200d)。按八问给结论:

1. 是什么问题:功能 PR(#3862)——dsh 客户端新增会话级 Context 标签页,/__bili/plugin/status 新增结构化 contextView 字段(压缩后真实发送构成 + fold/switch 事件轨迹)。我介入的直接原因是 CI 红:unused-exports 与 docs-sync 两个静态门禁失败,连带整个 test 矩阵被 skip。

2. 什么原因导致的:红门分两层——①PR 自己引入 6 个类型导出(context-view.ts 五个接口 + cache-ledger.ts 的 ModelSwitchStats)在仓库内无任何跨文件引用,触发 unused-exports 门禁;②docs-sync 失败是 master 既有漂移(CONFIGURATION.md 两行被 #3788/#3818 取代的过期行),与本 PR 无关,#3871/#3887 两个在途 PR 已认领。另:审查中发现一个 PR 自身的准确性缺陷——#1357 的 chain-advisory 响应和「旧版代理」响应一样带 panel 字符串,Context 标签页的降级阶梯会把「该会话还没有本地 session」误诊成「运行中的 bili 比安装配置旧,重启即可」。

3. 影响范围与严重程度:功能面=dsh 客户端用户多一个标签页、status 路由消费方多一个字段;CORS 回显只作用于已通过既有管理门禁(loopback + trusted-origin + anti-tunnel,admin.ts:189-206)的 origin。无用户损害(p3 功能类);CI 红曾阻塞合并。chain-advisory 误诊是边缘态下的 UX 文案错误(提示用户无谓重启),已修。

4. 怎么修复的:①6 个导出的修法(去掉 leading export,保留定义处命名,消费方经 buildContextView 返回类型拿形状)——我独立做出同样修复后,发现作者侧已推入字节级相同的 commit a90d59a77,遂丢弃我的重复 commit、把分支 reset 到它之上,不留重复工历史;②chain-advisory 修复(f261444):降级阶梯加 phase 判定,advisory 态读作「暂无数据」而非「旧构建」,附双向测试行(正:显示 not-yet;负:绝不出现 stale-build 文案);③清理 diff 噪音(a302420):测试文件里无关测试闭括号前的游离空格、CLIENTS.md 的行尾空格与 Kimi Code 标题前的游离空格。

5. 是否有回归问题:逐项过完触碰路径的既有行为——status 响应:纯增量字段,早退路径(chain-advisory/pre-first-request)不带该字段(JSON 省略),消费方按可选处理即可;集成测试逐字段断言既有字段仍在;systemPromptTokens 计算只是上提,取值不变,panel 文本不变。CORS:新回显与 #3100 HTML embed 路由同模式,Node fetch 客户端不发 Origin 头→无头→既有消费方零变化;负例已测(example.com→403)。cache-ledger 仅去 export 关键字,零运行时变化。dsh-native-client:openWebButton 上提到模块级,行为(文案/tooltip/跳转目标)不变,两处调用点同步更新;新标签页走既有 slots.inject 模式,宿主缺槽位时 fail-open;全程原生 DOM 无 iframe(owner 指令)。测试 harness 的 fake useState 补齐了 React updater 函数形态(setState(x=>x+1)),向真实 React 语义对齐,同文件其余测试全绿。kernel 未动。结论:未发现对既有行为的破坏。

6. 是否破坏缓存:不涉及。未触碰缓存/重试/游标/去重逻辑;buildSessionCacheReport 对 ledger 只读。性能注记:每次 status 读现在多建一次 ledger 报告(O(ledger 行数)),与既有 /__bili/cache-report 路由同成本档;轮询上限 10s/打开的标签页 + 插件按需读,可接受。

7. 是否改了设计方向:不转向。全部沿用既有模式:#3100 的 ACAO 回显先例、cache-report 的按需报告构建、slots.inject 注册、原生 DOM(owner 指令)。

8. 是否引入了新的配置:不涉及。无新 env/config key/CLI flag(PR 正文 config-surface: none,核实属实)。

有意行为变更披露(旧→新 + 原因):

  • GET /__bili/plugin/status:原先从不回显 access-control-allow-origin → 现对通过既有管理门禁且带 Origin 头的请求回显该 origin。原因:dsh 客户端标签页跨源读取;与 dsh admin UI returns no such management endpoint after profile auto-update #3100 完全同模式,不扩大门禁本身。
  • 同路由新增 contextView 字段(首请求前为 null/缺失)。
  • dsh 客户端新增 Context 标签页(conversation.view 槽位 order 30;宿主不支持该槽位时静默不出现)。

CI 现状与合并判定:推送后本分支 unused-exports 仅剩 1 条 = master 既有的 log-mask.ts: maskUrlForPublish,docs-sync 仍是 master 既有漂移——两者都由在途的 #3871/#3887 覆盖,我不重复修。等它们落 master 后本 PR rebase 一次,全部门禁 + test 矩阵即跑。本地定向验证(最终树):unused-exports 门禁仅剩上述既有项、typecheck 干净、build 干净、两套定向套件 22/22 全绿(含新增 chain-advisory 双向行)。

判定:代码审查通过,可合——唯一前置是 #3871/#3887 落 master 后 rebase 让静态门禁转绿(合并动作留给人,我不代合)。

awork added 2 commits October 20, 2026 08:52
#3811 exported it, but its only reference is maskUrlsForPublish in the same file (INTERNAL_ONLY), which has kept the master unused-exports lane red. Drop the export keyword instead of deleting: the function is live internal code.
Committed CONFIGURATION.md/CONFIGURATION.zh-CN.md carried both the pre-#3818 'unset (shadow)' storage.unified row and the pre-#3788 sqliteAutoDownload row alongside their replacements, so docs-sync's git-diff-after-generate could never be clean. Regenerated from the current seeds; the diff removes exactly those four stale rows.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

CI 红已修复并推回分支 2026-10-20_context-view-panel(a302420 → 9770dfc),两个独立标注 commit。新 head 上 unused-exports、docs-sync 两个 job 已转绿,test 分片在跑。七问如下:

  1. 是什么问题:CI 两个门禁 job 红——unused-exports 报 src/log-mask.ts 有一个没人用的导出;docs-sync 报配置文档与生成器输出不一致(generate 后 diff 非空)。
  2. 什么原因导致的:关键发现——两处都是 master 存量红,不是本 PR 引入的(本 PR 根本没碰这两个文件,master @39fb841d 的 CI run 39348249852 也恰好红在这两个 job)。① [security][web] Diagnostics blob prints private relay hosts verbatim in the config section — the "secrets masked" header only masks values, not hosts | 诊断包 config 段原样输出内网中转地址:标题说"密钥已遮蔽",但只遮值不遮主机 #3811 新增 maskUrlForPublish 时带了 export,但全仓唯一引用是同文件的 maskUrlsForPublish → INTERNAL_ONLY,自那次合入起 master 的 unused-exports 就一直红;② 提交的 CONFIGURATION.md/zh-CN 里 storage 表新旧两版行并存(storage.unified 旧行 "unset (shadow)" + 新行 "full";storage.sqliteAutoDownload 缺 SQLite unified storage degraded to legacy-only (bili v0.1.201, node v20.19.5, win32-x64) #3788 冷却说明的旧行),生成器按当前 seed 只出一套,diff 永远清不掉。
  3. 影响范围与严重程度:仅 CI 门禁层面,零运行时行为影响;严重程度 = 纯构建门禁(顺带说明:master 主干一直挂着这两处红,任何新 PR 都会连带变红)。
  4. 怎么修复的:按 checker 自身提示走最小修法——① maskUrlForPublish 去掉 export 关键字模块私有化(不删函数,它还有内部调用点);② 用当前 seed 重新生成配置文档,diff 恰好是删掉那 4 行陈旧重复行(EN+ZH 各 2),本地验证幂等。
  5. 是否有回归问题:无。删 export 前已 grep 确认全仓(src/tests/scripts)零外部引用;文档 diff 只删重复行,保留行与生成器输出逐字一致。两个门禁在提交后的树上本地复跑均为绿(exact CI check:generate 后 git diff --exit-code 干净 + unused-export gate clean)。
  6. 是否破坏缓存:不涉及。
  7. 是否改了设计方向 / 新配置:都没有,无新 env/配置项。

备注:这两处修复是本 PR 里明确标注的顺手小修(fix(ci)/fix(docs),各自独立 commit);PR 合入后 master 的同名红会一并治好。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

判定:本次 CI 告警是过期/重复触发,当前分支无需再修。 未推送任何新 commit。

核对结果(全部取自 GitHub API,非本地猜测):

  • 本条告警引用的是 run 39347862216,跑在旧 head a14be7c663 上(创建于 00:28:38Z,早于我上一轮修复)。该 commit 已被取代,不再是分支头。
  • PR 当前活 head 是 9770dfcb3(上一轮我推的修复)。在它上面:unused-exports = ✅ success、docs-sync = ✅ success,其余 guard 全绿,test 分片在正常跑(与上轮一致,未被我的改动触碰)。
  • 也就是说,这条告警报的两个 job(unused-exports、docs-sync)正是我上一轮已经修好并确认转绿的两个——它们红的只是那份已被取代的旧 run,不是现状。

结论:这是同一失败在旧 commit 上的重复自动触发。按本 lane「每 PR 至多 2 次尝试」的约束,我不再对已消失的失败做第二次修复/推送,避免无意义的重复 commit。等活 head 9770dfcb3 的 test 分片收尾即可;若那里再红,会是新的独立信号,届时另行处理。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

判定:又是过期触发,无需修复。 与上一条判定相同,补录证据:本条引用 run 39347995757,跑在旧 head a90d59a777(创建于 00:30:01Z,是分支上早于我修复的祖先 commit)。PR 当前活 head 仍为 9770dfcb3,unused-exports、docs-sync 均为 ✅ success。未推送任何新 commit;同一失败在旧 commit 上的重复告警不再消耗修复尝试。活 head 仅剩 test 分片在跑,若再红会是独立新信号,另行处理。

@ranxianglei
ranxianglei merged commit 55d1bd0 into master Oct 20, 2026
23 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.630-master.55d1bd0 (rolling master tag).

npm install -g billion-context@0.0.630-master.55d1bd0  # 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 pushed a commit that referenced this pull request Oct 21, 2026
…-drawing a card

The dsh conversation.view tab shipped in #3886 as a native re-rendering of
the proxy's contextView payload (categories bar + anchor + event trail).
It works, but the same data now lives in TWO places: the web UI session
page (compression tree, fold ledger, switch attribution) and this second
hand-drawn card that must be kept in lockstep with the kernel's buildStatusPanel
categorization and the cache-ledger fold trail forever.

Per owner direction, embed the web UI's own session page instead:
?embed=1&lang=..&theme=..#/session/<id> in the shared EMBED_HEIGHT frame —
one place for the data, zero duplicated rendering to maintain.

- session id resolved from the SAME status URL the /acp command uses
  server-side (/__bili/plugin/status sessionId field), so the tab can never
  frame a different session than the lane; string-nonempty guard, older or
  foreign builds answering without one fall through to honest hints
- full pre-flight discipline shared with the settings panel(): probeOrigin
  (+per-origin embed verdict cache, mid-session re-binds re-probe), #2559
  host-theme pin into the frame URL, #3816 remote-blocked notice extracted
  to renderRemoteBlock() so fix text/relay snippet/jump target cannot drift
  between the two placements
- state tree stays honest end-to-end: degraded (no proxy) / remote-blocked /
  stale-build (frozen pre-embed proxy) / waiting-first-request / connecting /
  not-yet; chain-advisory reads as not-yet, never as 'restart fixes it';
  first paint before verdict+id never frames anything
- i18n: card-only keys removed from both dictionaries (EN/ZH sets stay equal);
  remaining keys are the shared set
- tests: the #3862 pin is rewritten for the embedded face — happy path
  asserts the exact iframe src/title, 10s single poller, refresh click
  re-polls without duplicating the interval, unmount cancels; every degraded
  sub-state pinned (waiting / chain-advisory-not-stale / stale-route /
  unparseable-body→degraded / all-throws→degraded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

1 participant