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.
This commit is contained in:
1 parent
e51864d8d1
commit
ba34b5494f
4 files changed
+141
-27
No files matched your search
+26
-11
@@ -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<string, unknown>)
|
||||
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<string, unknown>): SidechatModelSelection | undefined {
|
||||
const { provider, model, reasoningEffort } = data
|
||||
|
||||
+14
-8
@@ -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),
|
||||
}
|
||||
|
||||
@@ -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' }),
|
||||
|
||||
@@ -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<string, unknown>; provider?: string; model?: string } = {}) {
|
||||
function agent(id: string, over: { events?: unknown[]; header?: Record<string, unknown>; 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<string, unknown> }
|
||||
// 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<string, unknown> }
|
||||
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<string, unknown> }
|
||||
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<string, unknown> }
|
||||
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')
|
||||
|
||||
Reference in new issue
Block a user