From ba34b5494fb1a7e7aaa5727ea560258675ef004e Mon Sep 17 00:00:00 2001 From: xun1241 Date: Wed, 16 Sep 2026 23:44:35 +0800 Subject: [PATCH] fix(sidechat): preserve non-default reasoningEffort and prevent stale effort leak 1. In parentModelSelection: respect header.adapterDefaults?.reasoningEffort, preserving explicit reasoningEffort in lastUsed and checking effort matching in sameSelection parity with DSH core. 2. In sidechat.start: explicitly clear childAgentOptions.reasoningEffort when parentSelection has no effort, preventing stale effort leak from parent.options. 3. Expand tests in sidechat-core.spec.ts and sidechat-routes.spec.ts. --- src/sidechat-core.ts | 37 ++++++++++----- src/sidechat-routes.ts | 22 +++++---- tests/sidechat-core.spec.ts | 24 ++++++++-- tests/sidechat-routes.spec.ts | 85 +++++++++++++++++++++++++++++++++-- 4 files changed, 141 insertions(+), 27 deletions(-) diff --git a/src/sidechat-core.ts b/src/sidechat-core.ts index 0f6a5109..a142882d 100644 --- a/src/sidechat-core.ts +++ b/src/sidechat-core.ts @@ -620,10 +620,9 @@ export interface SidechatModelSelection { * `parent.options` verbatim silently ships the stale creation default * (issue #368). * - * `request/header` contributes provider/model only: the header's effort may - * be an adapter default the core selection getter deliberately does not - * adopt, while a `model/selection` event is the user's explicit pick and - * carries its effort verbatim. + * A `request/header` contributes provider/model/effort, EXCEPT when its + * effort is marked as an adapter default (`adapterDefaults.reasoningEffort`) + * which the core selection getter deliberately does not adopt as a sticky pick. */ export function parentModelSelection( events: readonly SidechatLogEvent[], @@ -637,20 +636,36 @@ export function parentModelSelection( continue } if (event.type !== 'request/header') continue - const config = (dataOf(event).header as { config?: unknown } | undefined)?.config + const header = (dataOf(event).header as { + config?: unknown + adapterDefaults?: { reasoningEffort?: boolean } + } | undefined) + const config = header?.config if (config === null || typeof config !== 'object') continue const selection = looseSelection(config as Record) if (selection === undefined) continue - lastUsed = { provider: selection.provider, model: selection.model } - if ( - pending !== undefined - && pending.provider === selection.provider - && pending.model === selection.model - ) pending = undefined + const isDefaultEffort = header?.adapterDefaults?.reasoningEffort === true + lastUsed = { + provider: selection.provider, + model: selection.model, + ...(!isDefaultEffort && selection.reasoningEffort !== undefined ? { reasoningEffort: selection.reasoningEffort } : {}), + } + if (sameSelection(pending, lastUsed)) pending = undefined } return pending ?? lastUsed } +function sameSelection( + left: SidechatModelSelection | undefined, + right: SidechatModelSelection | undefined, +): boolean { + if (left === right) return true + if (left === undefined || right === undefined) return false + return left.provider === right.provider + && left.model === right.model + && left.reasoningEffort === right.reasoningEffort +} + /** Narrow one loose record into a model selection (non-empty strings only). */ function looseSelection(data: Record): SidechatModelSelection | undefined { const { provider, model, reasoningEffort } = data diff --git a/src/sidechat-routes.ts b/src/sidechat-routes.ts index 617be9e9..0eea40cb 100644 --- a/src/sidechat-routes.ts +++ b/src/sidechat-routes.ts @@ -273,6 +273,19 @@ export function buildSidechatApi(ctx: Context, live?: AssistantLiveBuffer): Side // would then claim and send that stale message BEFORE the boundary + // question. The marker keeps `ownEvents()` at the end-seed boundary, so // the inherited inbox replays to empty. + const targetEffort = parentSelection !== undefined + ? (parentSelection.reasoningEffort === undefined ? undefined : ReasoningEffortId(parentSelection.reasoningEffort)) + : parent.options.reasoningEffort + const childAgentOptions: CreateAgentOptions['agentOptions'] = { + ...parent.options, + ...(routeProvider === undefined ? {} : { provider: routeProvider }), + ...(routeModel === undefined ? {} : { model: routeModel }), + } + if (targetEffort !== undefined) { + childAgentOptions.reasoningEffort = targetEffort + } else { + delete childAgentOptions.reasoningEffort + } const options: CreateAgentOptions = { sessionId: childId, meta: { @@ -285,14 +298,7 @@ export function buildSidechatApi(ctx: Context, live?: AssistantLiveBuffer): Side }, seed: seed as unknown as readonly SessionEvent[], inheritedEventCount: SessionLogOffset(seed.length), - agentOptions: { - ...parent.options, - ...(routeProvider === undefined ? {} : { provider: routeProvider }), - ...(routeModel === undefined ? {} : { model: routeModel }), - ...(parentSelection?.reasoningEffort === undefined - ? {} - : { reasoningEffort: ReasoningEffortId(parentSelection.reasoningEffort) }), - }, + agentOptions: childAgentOptions, setup, signal: AbortSignal.timeout(CREATE_TIMEOUT_MS), } diff --git a/tests/sidechat-core.spec.ts b/tests/sidechat-core.spec.ts index 0fc84d03..84009c47 100644 --- a/tests/sidechat-core.spec.ts +++ b/tests/sidechat-core.spec.ts @@ -403,13 +403,23 @@ describe('parentModelSelection', () => { ])).toEqual({ provider: 'p2', model: 'm2', reasoningEffort: 'high' }) }) - it('lets a same-route request header consume the pending selection', () => { - // Consumed pending -> the header's route is current (effort dropped: the - // header's effort may be an adapter default the core getter does not adopt). + it('lets a same-route request header consume the pending selection preserving explicit effort', () => { + // Consumed pending -> the header's route is current (effort preserved when not an adapter default). expect(parentModelSelection([ ev('model/selection', 0, { provider: 'p2', model: 'm2', reasoningEffort: 'high' }), ev('request/header', 1, { header: { config: { provider: 'p2', model: 'm2', reasoningEffort: 'high' } } }), - ])).toEqual({ provider: 'p2', model: 'm2' }) + ])).toEqual({ provider: 'p2', model: 'm2', reasoningEffort: 'high' }) + }) + + it('drops adapter-default effort from request headers', () => { + expect(parentModelSelection([ + ev('request/header', 0, { + header: { + config: { provider: 'p1', model: 'm1', reasoningEffort: 'low' }, + adapterDefaults: { reasoningEffort: true }, + }, + }), + ])).toEqual({ provider: 'p1', model: 'm1' }) }) it('keeps a pending selection a different-route header cannot consume', () => { @@ -426,6 +436,12 @@ describe('parentModelSelection', () => { ])).toEqual({ provider: 'p1', model: 'm1' }) }) + it('preserves non-default effort on request header fallback', () => { + expect(parentModelSelection([ + ev('request/header', 0, { header: { config: { provider: 'p1', model: 'm1', reasoningEffort: 'high' } } }), + ])).toEqual({ provider: 'p1', model: 'm1', reasoningEffort: 'high' }) + }) + it('ignores malformed selection and header rows instead of throwing', () => { expect(parentModelSelection([ ev('model/selection', 0, { provider: '', model: 'm2' }), diff --git a/tests/sidechat-routes.spec.ts b/tests/sidechat-routes.spec.ts index bfc2c20f..1bffb58d 100644 --- a/tests/sidechat-routes.spec.ts +++ b/tests/sidechat-routes.spec.ts @@ -15,11 +15,15 @@ import { SIDE_BOUNDARY_PROMPT, SIDE_INJECTION_PLUGIN, SIDE_NEW_THREAD_TITLE, sid import type { Context } from '../src/context-types.ts' /** A fake live agent (inject/followup/cancel spied). */ -function agent(id: string, over: { events?: unknown[]; header?: Record; provider?: string; model?: string } = {}) { +function agent(id: string, over: { events?: unknown[]; header?: Record; provider?: string; model?: string; reasoningEffort?: string } = {}) { return { id, status: 'idle' as const, - options: { provider: over.provider ?? 'test', model: over.model ?? 'model-x' }, + options: { + provider: over.provider ?? 'test', + model: over.model ?? 'model-x', + ...(over.reasoningEffort !== undefined ? { reasoningEffort: over.reasoningEffort } : {}), + }, session: { id, header: { cwd: '/p', delegationDepth: 0, agentPreset: 'preset-a', ...over.header }, @@ -317,11 +321,84 @@ describe('sidechat.start', () => { await api['sidechat.start']({ sessionId: 'parent', question: 'side question' }) const options = services.create.mock.calls[0]![0] as { agentOptions: Record } - // Header effort is deliberately NOT adopted (it may be an adapter - // default the core selection getter does not honor). expect(options.agentOptions).toEqual({ provider: 'gemini', model: 'gemini-3.7-flash-high' }) }) + it('inherits reasoningEffort from request headers when not marked as adapter default', async () => { + const parent = agent('parent', { + provider: 'ark-code', + model: 'glm-5.3-flash', + events: [ + ev('request/header', 0, { + header: { + config: { provider: 'openrouter', model: 'ox-alpha', reasoningEffort: 'high' }, + }, + }), + ], + }) + const child = agent('child') + const services = happyServices(parent, child) + const api = buildSidechatApi(ctxWith(services)) + + await api['sidechat.start']({ sessionId: 'parent', question: 'side question' }) + + const options = services.create.mock.calls[0]![0] as { agentOptions: Record } + expect(options.agentOptions).toEqual({ + provider: 'openrouter', + model: 'ox-alpha', + reasoningEffort: 'high', + }) + }) + + it('drops adapter-default reasoningEffort from request headers', async () => { + const parent = agent('parent', { + provider: 'ark-code', + model: 'glm-5.3-flash', + events: [ + ev('request/header', 0, { + header: { + config: { provider: 'openrouter', model: 'ox-alpha', reasoningEffort: 'low' }, + adapterDefaults: { reasoningEffort: true }, + }, + }), + ], + }) + const child = agent('child') + const services = happyServices(parent, child) + const api = buildSidechatApi(ctxWith(services)) + + await api['sidechat.start']({ sessionId: 'parent', question: 'side question' }) + + const options = services.create.mock.calls[0]![0] as { agentOptions: Record } + expect(options.agentOptions).toEqual({ + provider: 'openrouter', + model: 'ox-alpha', + }) + }) + + it('clears stale parent options reasoningEffort when parent switches to a model without effort', async () => { + const parent = agent('parent', { + provider: 'oai', + model: 'o1', + reasoningEffort: 'high', + events: [ + ev('model/selection', 0, { provider: 'ark-code', model: 'glm-5.3-flash' }), + ], + }) + const child = agent('child') + const services = happyServices(parent, child) + const api = buildSidechatApi(ctxWith(services)) + + await api['sidechat.start']({ sessionId: 'parent', question: 'side question' }) + + const options = services.create.mock.calls[0]![0] as { agentOptions: Record } + expect(options.agentOptions).toEqual({ + provider: 'ark-code', + model: 'glm-5.3-flash', + }) + expect(options.agentOptions.reasoningEffort).toBeUndefined() + }) + it('rejects a non-running parent', async () => { const parent = agent('parent', { events: [] }) const child = agent('child')