From fe291fa0af209160ca75d3f2917c8043b255cec6 Mon Sep 17 00:00:00 2001 From: PerryLink Date: Sat, 7 Nov 2026 20:56:17 +0800 Subject: [PATCH] =?UTF-8?q?fix(sidechat):=20=E5=9C=A8=20setup=20=E9=87=8C?= =?UTF-8?q?=E8=A3=85=E5=9B=9E=20installModelSelection=EF=BC=8C=E5=88=87?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E5=90=8E=E6=96=B0=E6=A8=A1=E5=9E=8B=E6=94=B6?= =?UTF-8?q?=E5=88=B0=20[model=20changed]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0.2.2 把路由挪到 AgentModel 后,0.30.0 适配把 installModelSelection 一并删了;但该函数仍注册 agent/pre-step 监听来追加 [model changed] 提示,宿主 composeAgent 在两条分支都先装它、再 presets.mount。恢复为共享 threadSetup:5 个 setup 分支先 install 再 mount,并补 create / cold-resume 的监听回归测试。 --- src/sidechat-routes.ts | 32 +++++++++++++++++++++++++------- tests/sidechat-routes.spec.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/src/sidechat-routes.ts b/src/sidechat-routes.ts index b697fe2f..5256e13f 100644 --- a/src/sidechat-routes.ts +++ b/src/sidechat-routes.ts @@ -27,6 +27,7 @@ import { randomUUID } from 'node:crypto' import { createUserMessage, ReasoningEffortId, type ContentBlock, type UserMessage } from '@deepseek-ai/dsh-llm' import type { MessageId } from '@deepseek-ai/dsh-llm/brand' import { + installModelSelection, type Agent, type AgentSetup, type CreateAgentOptions, @@ -192,21 +193,38 @@ interface SidebarLlmService { }): Promise } +/** Build the setup every side thread shares: mount the preset (when one + * resolved) and install the host's model-selection pre-step listener. + * + * 0.2.2 moved ROUTING onto the agent's own `AgentModel`, but the + * `[model changed: …]` notice is still appended by the listener + * `installModelSelection` registers, and the host installs it in every one of + * its own setups (`api-session-controller`'s composeAgent, `dsh-acp`, + * `dsh-headless`). The 0.2.2 adaptation dropped it here (#1134), so the next + * model kept answering as if the turns above had used its own route. Install + * once per agent, before `presets.mount` — the host's order. */ +function threadSetup(mount?: (agentCtx: CordisContext) => Promise): AgentSetup { + return async (agentCtx: CordisContext) => { + installModelSelection(agentCtx) + if (mount !== undefined) await mount(agentCtx) + } +} + /** Resolve the parent's preset and build the child's composition setup - * (mirror of api-proxy's composeAgent minus the model-selection install — - * the child carries the parent's provider/model in agentOptions). */ + * (mirror of api-proxy's composeAgent: preset mount + model-selection install, + * while the child carries the parent's provider/model in agentOptions). */ async function composeChildSetup( ctx: Context, presetId: string | undefined, ): Promise<{ agentPreset?: string; setup: AgentSetup }> { const presets = ctx.get('agentPresets') as SidebarAgentPresetsService | undefined if (presets === undefined) { - return { setup: () => Promise.resolve() } + return { setup: threadSetup() } } const resolved = await presets.resolve(presetId) return { agentPreset: resolved.id, - setup: async (agentCtx: CordisContext) => { await presets.mount(agentCtx, resolved.id) }, + setup: threadSetup(async (agentCtx) => { await presets.mount(agentCtx, resolved.id) }), } } @@ -218,16 +236,16 @@ async function composePersistedSetup( ): Promise { const persistence = ctx.get('sessionPersistence') as SidebarSessionPersistenceService | undefined if (persistence === undefined) { - return () => Promise.resolve() + return threadSetup() } const inspected = await readColdThreadSession(persistence, childId) const presetId = resolvePresetId(inspected.header, inspected.events) const presets = ctx.get('agentPresets') as SidebarAgentPresetsService | undefined if (presets === undefined || presetId === undefined) { - return () => Promise.resolve() + return threadSetup() } const resolved = await presets.resolve(presetId) - return async (agentCtx: CordisContext) => { await presets.mount(agentCtx, resolved.id) } + return threadSetup(async (agentCtx) => { await presets.mount(agentCtx, resolved.id) }) } /** One text-block prompt (the thread boundary + question, or a follow-up). */ diff --git a/tests/sidechat-routes.spec.ts b/tests/sidechat-routes.spec.ts index c290db23..5776465e 100644 --- a/tests/sidechat-routes.spec.ts +++ b/tests/sidechat-routes.spec.ts @@ -152,6 +152,36 @@ describe('sidechat.start', () => { expect(get).toHaveBeenCalledOnce() }) + it('installs the model-switch listener in the create and cold-resume setups (#1134)', async () => { + // 0.2.2 kept the `[model changed: …]` notice in `installModelSelection`'s + // `agent/pre-step` listener even though routing moved onto `AgentModel`. + // The host installs it in every setup; a side thread that skips it leaves + // the next model unaware that the turns above used another route. + const services = happyServices(agent('parent'), agent('child')) + const api = buildSidechatApi(ctxWith(services)) + await api['sidechat.start']({ sessionId: 'parent', question: 'q' }) + const createSetup = (services.create.mock.calls[0] as unknown as [ + { setup: (ctx: unknown) => Promise }, + ])[0].setup + const on = vi.fn((_event: string) => () => {}) + await createSetup({ on }) + expect(on.mock.calls.filter(([type]) => type === 'agent/pre-step')).toHaveLength(1) + + // Cold resume builds its own setup from the persisted record; that branch + // must install the listener too. + const child = agent('child-resume') + const resumed = happyServices(undefined, child) + resumed.agents.get = vi.fn((_id: unknown) => undefined) + const resumeApi = buildSidechatApi(ctxWith(resumed)) + await resumeApi['sidechat.model']({ childId: 'child-resume', provider: 'p3', model: 'm3' }) + const resumeSetup = (resumed.resume.mock.calls[0] as unknown as [ + { setup: (ctx: unknown) => Promise }, + ])[0].setup + const onResume = vi.fn((_event: string) => () => {}) + await resumeSetup({ on: onResume }) + expect(onResume.mock.calls.filter(([type]) => type === 'agent/pre-step')).toHaveLength(1) + }) + it('stamps current cwd after a truncated seed so the real working-directory projection cannot restore an older cwd', async () => { const original = resolve('/original-project') const previous = resolve('/previous-execution')