Skip to content

Commit bf12e11

Browse files
bhavyausCopilot
andauthored
Model router: on follow-ups, switch to the route the message fits (#346799)
A follow-up used to need two answers before it switched: another route had to fit, and a separate scope answer had to say the work had moved on, with at least 0.7 probability. The scope answer often said "same" for related new work, for example explaining a library and then prototyping a feature in it, so the chat stayed. Follow-ups now follow the routes. A message that clearly fits another route (at least 0.6, leading the current route by at least 0.25) moves the chat. The cooldown and the per-chat switch limit stay. The scope question is no longer asked. Leaving it out doesn't change any pick in the follow-up eval. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3f2f1c7 commit bf12e11

4 files changed

Lines changed: 18 additions & 44 deletions

File tree

‎src/vs/platform/agentHost/node/copilot/copilotModelRouter.ts‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,12 @@ export interface IModelRouterPickRequest {
4343
readonly chat: string;
4444
}
4545

46-
export type ModelRouterScopeChange = 'same' | 'expanded' | 'different';
47-
4846
export interface IModelRouterPick {
4947
readonly model: string | undefined;
5048
readonly confidence: number;
5149
readonly probabilities: Readonly<Record<string, number>>;
5250
/** Expected effort rank, from 0 (low) to 4 (max). */
5351
readonly effortScore?: number;
54-
readonly scope?: { readonly kind: ModelRouterScopeChange; readonly probability: number };
5552
}
5653

5754
export const enum ModelRouterPickerFailure {
@@ -80,11 +77,9 @@ const CONTINUATION = /^\s*(ok(ay)?|yes|yep|sure|continue|proceed|next|go ahead|d
8077
/** How many earlier turns a follow-up decision sees. */
8178
const RECENT_TURNS = 4;
8279

83-
/** The thresholds a follow-up must clear to move a running chat. A switch discards the prompt cache, so staying is the default. */
80+
/** The thresholds a follow-up must clear to move a running chat to the route its message fits. */
8481
export const modelRouterSwitchPolicy = {
85-
/** How sure the picker must be that the work really moved on. */
86-
minScopeProbability: 0.7,
87-
/** How sure it must be about the new model. */
82+
/** How sure the picker must be about the new model. */
8883
minPickProbability: 0.6,
8984
/** How far the new model must lead the current one, so a near tie does not move the chat. */
9085
minLead: 0.25,
@@ -135,10 +130,10 @@ export interface IModelRouterSwitchInput {
135130
readonly switches: number;
136131
}
137132

138-
/** Decides whether a follow-up moves the chat. */
133+
/** Decides whether a follow-up moves the chat: it does when its message clearly fits another route. */
139134
export function decideModelRouterSwitch({ currentModel, pick, turnsSinceSwitch, switches }: IModelRouterSwitchInput): ModelRouterSwitchDecision {
140135
const policy = modelRouterSwitchPolicy;
141-
if (!pick.model || pick.model === currentModel || !pick.scope || pick.scope.kind === 'same' || pick.scope.probability < policy.minScopeProbability) {
136+
if (!pick.model || pick.model === currentModel) {
142137
return 'stay';
143138
}
144139
const pickProbability = pick.probabilities[pick.model] ?? pick.confidence;
@@ -257,7 +252,7 @@ export class CopilotModelRouter {
257252
return decision(router, picked, { ...switchedFrom(picked), ...preferenceOf(picked) }, [], effort);
258253
}
259254

260-
/** Keeps the chat on its model unless the work has clearly moved on and a switch is worth losing the cache. */
255+
/** Moves the chat when its message clearly fits another route, and otherwise keeps its model. */
261256
private async _routeFollowUp(context: IRouteContext, current: IResolvedModelRoute, currentEffort: string | undefined): Promise<IAgentTurnModelResolution> {
262257
const { request, router, routes } = context;
263258
const stay = decision(router, current, { kept: true }, [], router.effort === 'off' ? undefined : current.effort ?? currentEffort);
@@ -291,15 +286,15 @@ export class CopilotModelRouter {
291286
return stay;
292287
}
293288
const outcome = decideModelRouterSwitch({ currentModel: current.model.id, pick, ...switchHistory(previousTurns) });
294-
this._logService.info(`[ModelRouter] ${request.chat}: follow-up ${outcome}; current=${current.model.id} pick=${pick.model} scope=${pick.scope?.kind}/${pick.scope?.probability}`);
289+
this._logService.info(`[ModelRouter] ${request.chat}: follow-up ${outcome}; current=${current.model.id} pick=${pick.model} probabilities=${JSON.stringify(pick.probabilities)}`);
295290
const effort = router.effort === 'auto' && !current.effort
296291
? decideModelRouterEffort({ current: currentEffort, score: pick.effortScore, supported: current.supportedEfforts })
297292
: this._effortFor(context, current, undefined);
298293
const target = routes.find(route => route.model.id === pick.model);
299-
if (outcome === 'stay' || !target || !pick.scope) {
294+
if (outcome === 'stay' || !target) {
300295
return decision(router, current, { kept: true }, [], effort);
301296
}
302-
return decision(router, target, { switchedFrom: current.model.id, ...preferenceOf(target) }, [describeSwitch(router.name, pick.scope.kind, target.model.name)], this._effortFor(context, target, pick.effortScore));
297+
return decision(router, target, { switchedFrom: current.model.id, ...preferenceOf(target) }, [describeSwitch(router.name, target.model.name)], this._effortFor(context, target, pick.effortScore));
303298
}
304299
}
305300

@@ -319,10 +314,8 @@ function preferenceOf(route: IResolvedModelRoute): Partial<IModelRoutedInfo> {
319314
return { preference: line.length > PREFERENCE_CHARS ? `${line.slice(0, PREFERENCE_CHARS).trimEnd()}\u2026` : line };
320315
}
321316

322-
function describeSwitch(router: string, scope: ModelRouterScopeChange, model: string): string {
323-
return scope === 'different'
324-
? localize('modelRouter.switchedDifferent', "This looks like a different task, so the model router {0} moved this chat to {1}.", router, model)
325-
: localize('modelRouter.switchedExpanded', "This work has grown, so the model router {0} moved this chat to {1}.", router, model);
317+
function describeSwitch(router: string, model: string): string {
318+
return localize('modelRouter.switched', "This request fits another route, so the model router {0} moved this chat to {1}.", router, model);
326319
}
327320

328321
function describeFailure(router: string, error: unknown, model: string): string {

‎src/vs/platform/agentHost/node/copilot/copilotModelRouterPicker.ts‎

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { VSBuffer } from '../../../../base/common/buffer.js';
77
import { hash } from '../../../../base/common/hash.js';
88
import { agentDecisionErrorKind, type IAgentDecisionQuestion, type IAgentDecisionsRequest, type IAgentDecisionsResult } from '../../common/agentDecisions.js';
99
import { MODEL_ROUTER_DECISION_MODEL, MODEL_ROUTER_MAX_ROUTES } from '../../common/modelRouter.js';
10-
import { ModelRouterPickerError, ModelRouterPickerFailure, type IModelRouterPick, type IModelRouterPicker, type IModelRouterPickRequest, type ModelRouterScopeChange } from './copilotModelRouter.js';
10+
import { ModelRouterPickerError, ModelRouterPickerFailure, type IModelRouterPick, type IModelRouterPicker, type IModelRouterPickRequest } from './copilotModelRouter.js';
1111

1212
/** Routing holds up the turn, so a slow decision falls back to the router's first route. */
1313
const DECISION_TIMEOUT_MS = 5000;
@@ -21,12 +21,6 @@ export const modelRouterRequestLimits = {
2121

2222
export type ModelRouterAskDecisions = (request: IAgentDecisionsRequest) => Promise<IAgentDecisionsResult>;
2323

24-
const scopeCriteria: Readonly<Record<ModelRouterScopeChange, string>> = {
25-
same: 'The same work as the chat so far, or a small follow-up to it.',
26-
expanded: 'The work has grown substantially: many more files, harder reasoning, real ambiguity, or the current model is struggling with it.',
27-
different: 'A different task, unrelated to what the chat has been doing.',
28-
};
29-
3024
/** Without this, the decision model judges a request by its length and routes most of them to the lightest model. */
3125
const agentFraming = 'The request goes to an AI coding agent that works in the user\'s repositories with tools: it can read files, run commands and edit code. Judge the work the request needs to be done well, not how short the message is: a short message can start a long investigation.';
3226

@@ -60,7 +54,7 @@ export class DecisionsModelRouterPicker implements IModelRouterPicker {
6054
`decisions.ask failed: ${kind ?? (error instanceof Error ? error.name : 'unknown')}`,
6155
);
6256
}
63-
return toPick(result, request);
57+
return toPick(result);
6458
}
6559
}
6660

@@ -112,14 +106,6 @@ export function buildModelRouterDecisionsRequest(request: IModelRouterPickReques
112106
levels: effortCriteria.map(criterion => ({ label: criterion.slice(0, criterion.indexOf(':')), description: criterion })),
113107
});
114108
}
115-
if (followUp) {
116-
questions.push({
117-
kind: 'choice',
118-
name: 'scope',
119-
instructions: 'How does this message relate to the work the chat has been doing?',
120-
options: Object.entries(scopeCriteria).map(([value, description]) => ({ value, description })),
121-
});
122-
}
123109
let prompt = truncateToEscapedBytes(request.text, modelRouterRequestLimits.prompt);
124110
let recentTurns = followUp?.recentTurns.map(turn => {
125111
const prompt = truncateToEscapedBytes(turn.prompt, modelRouterRequestLimits.recentTurn / 2);
@@ -156,20 +142,15 @@ function describeRoute(candidate: IModelRouterPickRequest['candidates'][number])
156142
}
157143

158144
/** A tied or refused model answer is no pick, so the router falls back rather than choosing between equals. */
159-
function toPick(result: IAgentDecisionsResult, request: IModelRouterPickRequest): IModelRouterPick {
145+
function toPick(result: IAgentDecisionsResult): IModelRouterPick {
160146
const answer = (name: string) => result.answers.find(candidate => candidate.name === name);
161147
const model = answer('model');
162148
const effort = answer('effort');
163-
const scope = answer('scope');
164149
const clearModel = model?.kind === 'choice' && !model.tied ? model : undefined;
165-
const scopeKind = scope?.kind === 'choice' && !scope.tied && Object.hasOwn(scopeCriteria, scope.value) ? scope.value as ModelRouterScopeChange : undefined;
166150
return {
167151
model: clearModel?.value,
168152
confidence: clearModel?.confidence ?? 0,
169153
probabilities: model?.kind === 'choice' ? Object.fromEntries(model.probabilities.map(entry => [entry.value, entry.probability])) : {},
170154
...(effort?.kind === 'score' ? { effortScore: effort.score } : {}),
171-
...(request.followUp && scopeKind && scope?.kind === 'choice'
172-
? { scope: { kind: scopeKind, probability: scope.probabilities.find(entry => entry.value === scopeKind)?.probability ?? 0 } }
173-
: {}),
174155
};
175156
}

‎src/vs/platform/agentHost/test/node/copilotModelRouter.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ suite('CopilotModelRouter', () => {
8585

8686
test('a follow-up stays on its model without a decision model, or when switching is first-turn, the default', async () => {
8787
const previousTurns = [routedTurn({ router: 'daily', chosenModel: 'opus' })];
88-
const picker = new FakePicker({ model: 'haiku', confidence: 0.9, probabilities: { haiku: 0.9, opus: 0.1 }, scope: { kind: 'different', probability: 0.9 } });
88+
const picker = new FakePicker({ model: 'haiku', confidence: 0.9, probabilities: { haiku: 0.9, opus: 0.1 } });
8989
const results = [
9090
await resolve({ file: routerFile('switching: auto\n'), previousTurns }),
9191
await resolve({ file: routerFile(), previousTurns, picker }),
@@ -94,17 +94,17 @@ suite('CopilotModelRouter', () => {
9494
assert.strictEqual(picker.requests.length, 0);
9595
});
9696

97-
test('with switching auto, a follow-up switches when the work clearly moved on, sending recent turns as text', async () => {
97+
test('with switching auto, a follow-up moves to the route its message clearly fits, sending recent turns as text', async () => {
9898
const previousTurns = [routedTurn({ router: 'daily', chosenModel: 'opus' })];
99-
const picker = new FakePicker({ model: 'haiku', confidence: 0.9, probabilities: { haiku: 0.9, opus: 0.1 }, scope: { kind: 'different', probability: 0.9 } });
99+
const picker = new FakePicker({ model: 'haiku', confidence: 0.9, probabilities: { haiku: 0.9, opus: 0.1 } });
100100
const result = await resolve({ file: routerFile('switching: auto\n'), previousTurns, picker, text: 'rename this variable' });
101101
assert.deepStrictEqual({ selection: result.selection?.id, switchedFrom: result.routed?.switchedFrom, recent: picker.requests[0].followUp?.recentTurns }, {
102102
selection: 'haiku', switchedFrom: 'opus', recent: [{ prompt: 'earlier', response: '' }],
103103
});
104104
});
105105

106106
test('the switch policy stays on near ties and holds in long chats', () => {
107-
const pick: IModelRouterPick = { model: 'haiku', confidence: 0.9, probabilities: { haiku: 0.9, opus: 0.1 }, scope: { kind: 'expanded', probability: 0.9 } };
107+
const pick: IModelRouterPick = { model: 'haiku', confidence: 0.9, probabilities: { haiku: 0.9, opus: 0.1 } };
108108
assert.deepStrictEqual([
109109
decideModelRouterSwitch({ currentModel: 'opus', pick, turnsSinceSwitch: undefined, switches: 0 }),
110110
decideModelRouterSwitch({ currentModel: 'opus', pick: { ...pick, probabilities: { haiku: 0.6, opus: 0.5 } }, turnsSinceSwitch: undefined, switches: 0 }),

‎src/vs/platform/agentHost/test/node/copilotModelRouterPicker.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ suite('DecisionsModelRouterPicker', () => {
4040
firstContext: { kind: 'text', text: 'A user sent this message to an AI coding agent:\n\nplan the migration' },
4141
firstQuestions: ['model', 'effort'],
4242
followUpTurns: [{ prompt: 'earlier', response: 'done' }],
43-
followUpQuestions: ['model', 'effort', 'scope'],
43+
followUpQuestions: ['model', 'effort'],
4444
});
4545
});
4646

0 commit comments

Comments
 (0)