merge fix/022-model-selection-dedupe: AgentModel.select 已是落盘动作,去掉重复 append

This commit is contained in:
Menghuan1918 committed 2026-11-04 22:20:42 +08:00
commit b10e3e9c4f
2 files changed
+30 -18

No files matched your search

+14 -10
View File
@@ -484,13 +484,16 @@ function brandedSelection(selection: SidechatModelSelection): {
}
/**
* Hand the agent the model-selection seam, once per live agent: the ref prompt
* assembly snapshots and request routing applies. Seeded from the thread's
* recorded selection, so a resumed thread keeps the model the user picked
* instead of reverting to the deployment default.
* Claim the thread's model seam once per live agent. DSH 0.2.2 hands every agent
* its own `AgentModel` (`agent.model`), whose `select()` records the choice in
* the log AND applies it to the next step — there is nothing to install, only a
* handle to remember. Claiming SEEDS it from the thread's recorded selection so
* a resumed thread keeps the model the user picked instead of reverting to the
* deployment default; `select()` is a no-op when that choice is already the
* recorded one, so re-seeding never appends a duplicate.
* @param agent - the live thread agent.
* @param childId - thread identity (the ref's key).
* @returns the installed (or already installed) ref.
* @param childId - thread identity (the map key).
* @returns the agent's model handle.
*/
function ensureModelSelection(agent: Agent, childId: string): AgentModelHandle {
const existing = threadModelRefs.get(childId)
@@ -974,10 +977,11 @@ export function buildSidechatApi(ctx: Context, live?: AssistantLiveBuffer, signa
}
const selection = await normalizeSelection(ctx, requested)
const agent = await ensureLiveThread(ctx, childId)
// The host's own commit shape (`SessionController.selectForNextRequest`):
// the durable intent lands in the log, and the agent-scoped ref is what
// prompt assembly and request routing read for the NEXT step.
agent.session.append('model/selection', brandedSelection(selection))
// `AgentModel.select()` IS the host's commit shape in 0.2.2: it appends
// `model/selection` when the choice differs and applies it to the next
// step (a no-op for the same choice). Appending here as well would record
// every switch twice — the 0.2.1 installable ref needed both calls, this
// one does not.
ensureModelSelection(agent, childId).select(brandedSelection(selection))
return { accepted: true as const, selection }
},
+16 -8
View File
@@ -1027,7 +1027,7 @@ describe('sidechat.models and sidechat.model', () => {
expect(result.failures).toEqual([{ id: 'host', message: 'boom' }])
})
it('normalizes, logs and installs a selection for the live thread', async () => {
it('normalizes and applies a selection for the live thread', async () => {
const child = agent('child-model', { events: threadLog() })
const services = happyServices(undefined, child)
const resolveCallConfig = vi.fn(async () => ({ provider: 'p2', model: 'm2', reasoningEffort: 'high' }))
@@ -1038,11 +1038,14 @@ describe('sidechat.models and sidechat.model', () => {
selection: { provider: 'p2', model: 'm2', reasoningEffort: 'high' },
})
expect(resolveCallConfig).toHaveBeenCalledTimes(1)
expect(child.session.append).toHaveBeenCalledWith('model/selection', {
// `select()` is the durable commit in 0.2.2: it appends `model/selection`
// itself, so the route must NOT append a second one.
expect(child.model.select).toHaveBeenCalledWith({
provider: 'p2',
model: 'm2',
reasoningEffort: 'high',
reasoningEffort: expect.anything(),
})
expect(child.session.append).not.toHaveBeenCalled()
// The thread is claimed once: the first switch both SEEDS the agent's model
// from the thread's recorded selection and applies the requested one, so
// the second switch adds exactly one more select — no re-seed, no second
@@ -1050,7 +1053,6 @@ describe('sidechat.models and sidechat.model', () => {
const selections = child.model.select.mock.calls.length
expect(selections).toBe(2)
await api['sidechat.model']({ childId: 'child-model', provider: 'p2', model: 'm2' })
expect(child.session.append).toHaveBeenCalledTimes(2)
expect(child.model.select.mock.calls.length).toBe(selections + 1)
})
@@ -1063,17 +1065,23 @@ describe('sidechat.models and sidechat.model', () => {
}))
await expect(api['sidechat.model']({ childId: 'child', provider: 'p9', model: 'm9' }))
.rejects.toMatchObject({ code: 'bad-request', status: 400 })
expect(child.model.select).not.toHaveBeenCalled()
expect(child.session.append).not.toHaveBeenCalled()
})
it('cold-resumes the thread before installing a selection', async () => {
const child = agent('child', { events: threadLog() })
it('cold-resumes the thread before applying a selection', async () => {
// Its own thread id: the claimed-thread map is module state, so reusing a
// previously claimed id would (correctly) skip the first-claim seeding.
const child = agent('child-p3', { events: threadLog() })
const services = happyServices(undefined, child)
services.agents.get = vi.fn((_id: unknown) => undefined)
const api = buildSidechatApi(ctxWith(services))
await api['sidechat.model']({ childId: 'child', provider: 'p3', model: 'm3' })
await api['sidechat.model']({ childId: 'child-p3', provider: 'p3', model: 'm3' })
expect(services.resume).toHaveBeenCalledTimes(1)
expect(child.session.append).toHaveBeenCalledWith('model/selection', { provider: 'p3', model: 'm3' })
// The agent's own model handle records and applies the choice; the route
// does not append the event a second time.
expect(child.model.select).toHaveBeenCalledWith({ provider: 'p3', model: 'm3' })
expect(child.session.append).not.toHaveBeenCalled()
})
})