Skip to content

fix(dsh): bind llm.resolveModelInfo to its service receiver — every window resolve throws, stripping x-bili-plugin-context-window - #1943

Merged
ranxianglei merged 1 commit into
ranxianglei:masterfrom
Dclef:2026-10-03_dsh-bind-resolve-model-info
Oct 3, 2026
Merged

ranxianglei merged 1 commit into
ranxianglei:masterfrom
Dclef:2026-10-03_dsh-bind-resolve-model-info

Conversation

@Dclef

@Dclef Dclef commented Oct 2, 2026 •

Copy link
Copy Markdown

Root cause

refreshModelInfo in src/agent/dsh-native.ts reads the resolver off the host's llm service object and then calls it detached:

const resolve = svc.llm?.resolveModelInfo;
...
void Promise.resolve().then(() => resolve(provider, model))

dsh-llm registers resolveModelInfo as a class method whose first statement is return this.resolveModelInfoFor(this.registration(provider), model, signal). With no receiver, this is undefined, so every resolve rejects with:

Cannot read properties of undefined (reading 'resolveModelInfoFor')

This is unconditional — every model, every provider, every process start. There is no configuration that avoids it.

The failure is then absorbed by the failure-shaped cache (modelInfo.cached = { provider, model }, no window), so headersFor never stamps x-bili-plugin-context-window. The proxy then sizes the session against its unconfigured fallback and logs:

[window] model=space-bunny agent=dsh sent no context window (x-bili-plugin-context-window absent, no matching runtime-info) — host-configured limit not reaching the proxy; sizing against default

Why the existing fixes do not cover it

#1812 / #1836 added a retry cadence so a transient failure heals after a 30s cooldown. That cannot help here: the call fails deterministically, so every retry fails identically. The symptom is timing-independent, which is also why the error is easy to misread as a boot race.

The type declaration at dsh-native.ts:84 declares llm as a structural object with an optional method property. It reads like a plain function, but in production it is a class instance whose method needs its receiver — TypeScript cannot catch this, because the declared shape says nothing about the runtime receiver.

Note that the error text names resolveModelInfoFor, an inner dsh-llm method. That points a reader at the wrong module, which is why searching for the error text (or for resolveModelInfo) surfaces #1929 / #1849 but nothing matching this.

The fix

One line, at the extraction site:

const resolve = svc.llm?.resolveModelInfo?.bind(svc.llm);

No signature change, no config-surface change, no wire change.

Regression test

tests/dsh-native.test.ts — apply() runtime-info (#1942): binds resolveModelInfo to its service receiver, so a class-shaped llm service still stamps the window header

The stub is a real class whose resolveModelInfo reaches its own state through this, mirroring dsh-llm. A plain object literal cannot detect this bug (it does not depend on this), which is why the #955 / #956 / #1812 suites all pass against the broken code today.

  • pre-fix: fails with resolveModelInfo lost its service receiver: Cannot read properties of undefined (reading 'capacity')
  • post-fix: passes, stamping x-bili-plugin-model, x-bili-plugin-context-window: 1000000 and x-bili-plugin-max-output

Verification

  • npm run typecheck — clean
  • node --import tsx --test tests/dsh-native.test.ts — 43 tests, 43 pass, 0 fail (3.3s). This is the file holding the new test; the rest of the suite was not run. The change is confined to refreshModelInfo, whose only callers are headersFor in this same file, so this file is the enclosing blast radius.
  • git diff --check — clean

Impact

The dsh client now reports each model's real window through POST /__bili/plugin/runtime-info on its own, taken from the host model catalog. A per-model modelContextLimit workaround in the proxy config stops being necessary, and a newly added model needs no entry anywhere.

Note on scope

The boot-order race described in the follow-up comment is a separate defect with a different root cause (host initialization order) and a different fix (invalidate the cache on llm/adapters-updated). It is deliberately not addressed here.

refreshModelInfo detached the host's llm.resolveModelInfo and called it without a
receiver. dsh-llm registers it as a class method whose first statement is
this.resolveModelInfoFor(...), so every resolve rejected with

  Cannot read properties of undefined (reading 'resolveModelInfoFor')

for every provider and every model. The failure-shaped cache then stored
{provider, model} with no window, so headersFor never stamped
x-bili-plugin-context-window and the proxy sized the session against its
unconfigured fallback. ranxianglei#1812's cooldown retry cannot help: the call fails
deterministically, so each retry fails the same way.

The error text names an inner dsh-llm method, which points the search away
from the detached call site, so no existing report covers this.

Fixes ranxianglei#1942
@Dclef

Dclef commented Oct 2, 2026

Copy link
Copy Markdown
Author

Verification scope

Correcting the Verification section above to match what was actually run on this head:

  • npm run typecheck — clean
  • node --import tsx --test tests/dsh-native.test.ts — 43 tests, 43 pass, 0 fail (3.3s). This is the file containing the new test; the rest of the suite was not run.
  • git diff --check — clean

The change is confined to refreshModelInfo in src/agent/dsh-native.ts, and every caller of it lives in this same file (headersFor at :816/:853/:861), so the file-level suite is the enclosing blast radius. Flagging the narrower scope explicitly rather than claiming a full-suite run.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review of #1943 (external-contributor PR — review + verification only, per repo policy I am not pushing to your branch)

Fetched head cb48cd84 (base = current master 38a47589, fresh base, single commit, 2 files, +56/−1). Verdict up front: direction correct, fix correct, regression test has teeth — mergeable once the last CI jobs land. One cosmetic nit below.

① Original-issue direction check (verified against master source):

  • src/agent/dsh-native.ts:84 — the structural llm?: { resolveModelInfo?: … } type declaration is exactly as described; it says nothing about the runtime receiver.
  • src/agent/dsh-native.ts:183 — const resolve = svc.llm?.resolveModelInfo; extracted, then called detached at :189-190. Confirmed this is the only extract-then-call site for a host-service method in the tree: the sibling agentDefaultModel.currentSelection is invoked inline with its receiver kept (svc.agentDefaultModel?.currentSelection?.() at :163/:175), and a repo-wide scan found no other instance of this bug class.
  • Failure chain confirmed end-to-end: detached call rejects → caught at :204-209 into the failure-shaped cache { provider, model } (no window) → headersFor never stamps x-bili-plugin-context-window → the exact symptom log quoted in the issue exists verbatim at src/server.ts:1737.
  • The 130k上下文窗口被占满触发无效压缩越压越大 #1812/fix(dsh): retry failed model-window resolves — one boot race no longer strips x-bili-plugin-context-window #1836 retry cadence genuinely cannot heal this: the rejection is deterministic, so every cooldown retry fails identically. Your "timing-independent → misread as boot race" framing is right, and the scoping note (boot-order race = separate defect, out of scope) is correctly drawn.

② Regression audit (existing behaviors on the touched path):

③ Code: one-line fix at the extraction site — the decision point, not a mask. The 5-line comment documenting why the bind is required (TS structurally cannot express the receiver requirement) is a justified exception to the no-comments rule.

④ Test validity — verified both directions locally:

  • Pre-fix: checked out master's src/agent/dsh-native.ts under the PR's test file and ran the new test alone → fails with exactly your claimed message: resolveModelInfo lost its service receiver: Cannot read properties of undefined (reading 'capacity').
  • Post-fix (PR head): npm run typecheck clean; node --import tsx --test tests/dsh-native.test.ts → 43 pass / 0 fail; git diff --check clean.
  • The class-shaped stub (method reaches state through this.capacity()) is precisely the shape that detects this bug while object literals cannot — good design, and your explicit correction of the Verification section to file-level scope (rather than claiming a full-suite run) is exactly the honesty this repo asks for; CI carries the full suite.

⑤ Diff hygiene: clean, except one cosmetic nit — two comment lines in the new test contain a literal TAB that splits the word "this": it does not depend on <TAB>his. and // <TAB>his, so a detached call throws… (verified in the committed blob; git diff --check does not flag mid-comment tabs, hence the green). Functionally inert; a tiny fixup commit would be nice-to-have, not a blocker — owner's call.

CI at review time: 12/14 check runs green, including the full e2e set (dsh headless lane, codex, pi, opencode, hermetic registry) + build + version-guard; 2 test-matrix jobs still running at the time of writing.


中文结构化结论(七问)

  1. 是什么问题:dsh 原生插件把宿主 llm 服务的 resolveModelInfo 方法摘下来脱离接收者调用,导致每次窗口解析必然抛错,x-bili-plugin-context-window 头永远打不上,代理只能按默认兜底值给会话定窗。
  2. 什么原因导致的:根因是「结构性类型声明 + 摘取后调用」丢失运行时接收者——TS 类型无法表达 this 依赖;确定性失败又被失败形缓存(130k上下文窗口被占满触发无效压缩越压越大 #1812 的重试节奏治不了确定性失败)吸收,症状与时间无关,易被误读为启动竞态。
  3. 怎么修复的:在摘取点一行 bind(svc.llm),恢复接收者;不改签名、不改线协议、不改配置面。修在决策点而非下游打掩码。
  4. 是否回归问题:无。对对象字面量服务(现有全部 stub)bind 是行为无操作;守卫分支、失败形缓存、重试节奏、runtime-info 上报形状逐一核对均未变;全仓扫描确认同类 bug 仅此一处(currentSelection 是内联调用,接收者完好)。
  5. 是否破坏缓存:不涉及缓存失效策略;实际效果是恢复真实窗口值进入头/runtime-info,反而修正了此前的兜底定窗。
  6. 是否改了设计方向:不涉及。同一机制内修接收者;boot-order 竞态按你的说明刻意留作独立缺陷,边界划得正确。
  7. 是否引入新配置:不涉及。

判定:可合(待最后 2 个 test-matrix CI 任务转绿;tab 注释小疵可选修,不阻塞)。合并由人执行。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 CI 全绿确认 — head cb48cd84 的 14/14 check runs 全部 success(含完整 test matrix:ubuntu 22/24 + windows 22/24、e2e 全套、build、version-guard)。上一条 review 中「待最后 2 个 test-matrix 任务转绿」的条件已满足。

结论维持:可合,无回归。合并由人执行;tab 注释小疵仍为可选修,不阻塞。

@ranxianglei
ranxianglei merged commit 50227c8 into ranxianglei:master Oct 3, 2026
14 checks passed
ranxianglei added a commit that referenced this pull request Oct 3, 2026
…ot exist on the host (#1961)

pi v0.99.1 ModelRegistry exposes find(provider, modelId) (delegating to
the private runtime.getModel); there is no public getModel, so
ctx.modelRegistry?.getModel?.(...) always short-circuited to undefined —
tier ① (the only tier that can resolve ownership in the issue's own
lane: manual /bili/ wrapping, no env, no rewrite manifest) was dead code
on the real host. Tests passed because the stubs invented the same
non-existent method; they are now shaped like the real registry surface,
and a mutation check confirms the #1961 repro test fails against the old
call. Same failure class as #1943: object-literal stubs cannot catch
host-API drift.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants