From d163ab2ec10dd9a7b21ebdbc9beb15d844668fb9 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 02:05:02 +0800 Subject: [PATCH] fix(dsh): bind llm.resolveModelInfo to its service receiver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit dsh registers resolveModelInfo as a class method that delegates through its receiver (this.resolveModelInfoFor(this.registration(provider), …)). refreshModelInfo extracted it as a bare property and called it detached, dropping the receiver (`this === undefined` in strict mode) so every resolve rejected with "Cannot read properties of undefined (reading 'resolveModelInfoFor')" and the failure-shaped cache stripped x-bili-plugin-context-window for 100% of providers/models on every process start. Bind the receiver at the extraction site; harmless if a host ever supplies a plain function that ignores `this`. Regression test drives a real class-instance resolver whose method depends on `this` (object-literal stubs never do, which is why the existing #955/#956/#1812 suites passed despite the detached call). Verified: pre-fix the test times out waiting for the window header; post-fix it stamps model/window/max-output. Fixes #1942. --- src/agent/dsh-native.ts | 8 ++++++- tests/dsh-native.test.ts | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index a3a537c8e0..820574ee51 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -213,7 +213,13 @@ function refreshModelInfo(origin: string | undefined): void { if (modelInfo.cached.contextWindow !== undefined) return; if (modelInfo.retryAt !== undefined && Date.now() < modelInfo.retryAt) return; } - const resolve = svc.llm?.resolveModelInfo; + // #1942: dsh registers resolveModelInfo as a class method that delegates + // through its receiver (this.resolveModelInfoFor(this.registration(provider), …)). + // Extracting it as a bare property detaches `this` (undefined in strict mode), + // so every resolve rejects and the failure cache strips x-bili-plugin-context-window + // for 100% of providers/models. Bind the receiver at the extraction site; harmless + // if a host ever supplies a plain function that ignores `this`. + const resolve = svc.llm?.resolveModelInfo?.bind(svc.llm); if (resolve === undefined) { modelInfo.cached = { provider, model }; return; diff --git a/tests/dsh-native.test.ts b/tests/dsh-native.test.ts index c6b8e01054..5d4fafb0cb 100644 --- a/tests/dsh-native.test.ts +++ b/tests/dsh-native.test.ts @@ -975,6 +975,52 @@ test("apply() runtime-info (#1812): a failed window resolve retries after the co } }); +test("apply() runtime-info (#1942): a receiver-dependent resolver is bound, not called detached", async () => { + const proxy = await startMockProxy([]); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-bind-")); + try { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: proxy.origin }, async () => { + _resetRegisterForTest(proxy.origin); + const ctx = mockCtx(); + // dsh registers resolveModelInfo as a class method that delegates + // through its receiver; the other suites' object-literal stubs never + // depend on `this`, which is why they pass even when the call is + // detached. This fixture reproduces the real host shape. + class ReceiverLlm { + resolveModelInfo(_provider: string, _model: string): Promise<{ context?: { contextWindow?: number }; defaultMaxTokens?: number } | undefined> { + return Promise.resolve(this.inner()); + } + inner(): { context: { contextWindow: number }; defaultMaxTokens: number } { + return { context: { contextWindow: 1_000_000 }, defaultMaxTokens: 65536 }; + } + } + const llmService = new ReceiverLlm(); + // the fixture genuinely needs its receiver: a bare-property call + // detaches `this` and rejects before any catalog logic runs + const detached = llmService.resolveModelInfo; + await assert.rejects(Promise.resolve().then(() => detached("workbuddy", "space-bunny")), /Cannot read properties of undefined/); + ctx.setModelServices(llmService, { currentSelection: () => ({ provider: "workbuddy", model: "space-bunny" }) }); + apply(ctx); + await waitFor(() => ctx.registeredTools.length === 1, "manifest tool registration (bind)"); + ctx.setInitiator({ session: { id: "session-bind" } }); + // pre-fix the detached call threw, latched the failure-shaped cache, + // and the window header never appeared; bound, the first resolve commits it + await waitFor(() => { + const h = _stateHeadersForTest()?.("http://example.test/v1/chat/completions"); + return h?.["x-bili-plugin-context-window"] === "1000000"; + }, "bound resolver stamped the context-window header"); + const headers = _stateHeadersForTest()?.("http://example.test/v1/chat/completions"); + assert.equal(headers?.["x-bili-plugin-model"], "space-bunny"); + assert.equal(headers?.["x-bili-plugin-context-window"], "1000000"); + assert.equal(headers?.["x-bili-plugin-max-output"], "65536"); + }); + } finally { + proxy.close(); + rmrf(home); + _resetRegisterForTest(undefined); + } +}); + test("apply() /acp pre-first-request (#955): renders the runtime-table entry before any model request", async () => { const pre = { ok: true,