Skip to content

Commit 93468cb

Browse files
authored
fix(telegram): follow configured model runtime (#133125)
Telegram model buttons now adopt the selected model's configured runtime after stating that effect before the click. This removes stale compatible session runtime overrides, reports the effective runtime, and reuses the shared resolved runtime for follow-up and context handling. Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent c04bee4 commit 93468cb

5 files changed

Lines changed: 86 additions & 49 deletions

File tree

‎extensions/telegram/src/bot-handlers.callback-router.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -582,13 +582,13 @@ async function handleTelegramModelCallback(params: {
582582
totalPages,
583583
modelNames,
584584
});
585-
const text = formatModelsAvailableHeader({
585+
const text = `${formatModelsAvailableHeader({
586586
provider,
587587
total: models.length,
588588
cfg: runtimeCfg,
589589
agentDir: resolveAgentDir(runtimeCfg, sessionState.agentId),
590590
sessionEntry: sessionState.sessionEntry,
591-
});
591+
})}\nSelecting a model also applies its configured runtime.`;
592592
await retryModelAction(() => editMessageWithButtons(text, buttons));
593593
return true;
594594
}
@@ -666,7 +666,7 @@ async function handleTelegramModelCallback(params: {
666666
provider: selection.provider,
667667
model: selection.model,
668668
isDefault: isDefaultSelection,
669-
runtime: { kind: "unchanged" },
669+
runtime: { kind: "clear" },
670670
},
671671
markLiveSwitchPending: true,
672672
}),
@@ -687,13 +687,10 @@ async function handleTelegramModelCallback(params: {
687687
const actionText = isDefaultSelection
688688
? "reset to default"
689689
: `changed to <b>${escapeHtml(selection.provider)}/${escapeHtml(selection.model)}</b>`;
690-
const runtimeText =
691-
applied.runtimeChange?.kind === "clear"
692-
? "Runtime reset to configured policy."
693-
: "Runtime unchanged.";
690+
const runtimeText = `Runtime set to <b>${escapeHtml(applied.agentRuntime)}</b> from configured policy.`;
694691
const scopeText = isDefaultSelection
695692
? `Session model selection cleared.${defaultAuthProfileNotice ? ` ${defaultAuthProfileNotice}` : ""} ${runtimeText} New replies use the agent's configured default.`
696-
: `Session-only model selection. ${runtimeText} Use /model ${escapeHtml(selection.provider)}/${escapeHtml(selection.model)} --runtime &lt;runtime&gt; -s to switch harnesses. The agent default in openclaw.json is unchanged. This chat keeps the model selection across /new and /reset; use /model default -s to clear the session model selection.`;
693+
: `Session-only model selection. ${runtimeText} The agent default in openclaw.json is unchanged. This chat keeps the model selection across /new and /reset; use /model default -s to clear the session model selection.`;
697694
await editMessageWithButtons(`✅ Model ${actionText}\n\n${scopeText}`, [], {
698695
parse_mode: "HTML",
699696
});

‎extensions/telegram/src/bot.create-telegram-bot.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5667,7 +5667,7 @@ describe("createTelegramBot", () => {
56675667
expect(editMessageTextSpy).toHaveBeenCalledTimes(1);
56685668
const finalEditMessageText = editMessageTextSpy.mock.calls.at(-1)?.[2];
56695669
expect(typeof finalEditMessageText === "string" ? finalEditMessageText : "").toContain(
5670-
"Session-only model selection. Runtime unchanged.",
5670+
"Session-only model selection. Runtime set to <b>codex</b> from configured policy.",
56715671
);
56725672
expect(
56735673
editMessageTextSpy.mock.calls.some((call) =>

‎extensions/telegram/src/bot.test.ts‎

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ function makeModelPickerConfig(
334334
overrides: {
335335
config?: Omit<OpenClawConfig, "agents" | "channels" | "session">;
336336
defaultModel?: string;
337-
models?: Record<string, Record<string, never>>;
337+
models?: Record<string, { agentRuntime?: { id: string } }>;
338338
omitModels?: boolean;
339339
telegram?: TelegramChannelConfig;
340340
} = {},
@@ -2623,7 +2623,7 @@ describe("createTelegramBot", () => {
26232623
`${CHECK_MARK_EMOJI} Model reset to default`,
26242624
);
26252625
expect(String(firstEditMessageTextArg(2))).toContain(
2626-
"Session model selection cleared. Runtime unchanged. New replies use the agent's configured default.",
2626+
"Session model selection cleared. Runtime set to <b>openclaw</b> from configured policy. New replies use the agent's configured default.",
26272627
);
26282628

26292629
const entry = readOnlySessionEntry(storePath);
@@ -2678,7 +2678,7 @@ describe("createTelegramBot", () => {
26782678
expect(entry?.modelOverride).toBeUndefined();
26792679
expect(entry?.agentRuntimeOverride).toBeUndefined();
26802680
expect(String(firstEditMessageTextArg(2))).toBe(
2681-
`${CHECK_MARK_EMOJI} Model reset to default\n\nSession model selection cleared. Runtime reset to configured policy. New replies use the agent's configured default.`,
2681+
`${CHECK_MARK_EMOJI} Model reset to default\n\nSession model selection cleared. Runtime set to <b>openclaw</b> from configured policy. New replies use the agent's configured default.`,
26822682
);
26832683
});
26842684

@@ -2707,6 +2707,7 @@ describe("createTelegramBot", () => {
27072707
defaultModel: "gpt-5",
27082708
callbackData: "mdl_sel_openai/gpt-5",
27092709
outcomeText: "Compatible auth profile retained.",
2710+
expectedRuntime: "codex",
27102711
expectedProfile: "team:prod",
27112712
},
27122713
{
@@ -2716,6 +2717,7 @@ describe("createTelegramBot", () => {
27162717
defaultModel: "claude-sonnet-4-5",
27172718
callbackData: "mdl_sel_anthropic/claude-sonnet-4-5",
27182719
outcomeText: "Incompatible auth profile cleared.",
2720+
expectedRuntime: "openclaw",
27192721
expectedProfile: undefined,
27202722
},
27212723
])(
@@ -2726,6 +2728,7 @@ describe("createTelegramBot", () => {
27262728
defaultModel,
27272729
callbackData,
27282730
expectedProfile,
2731+
expectedRuntime,
27292732
outcomeText,
27302733
}) => {
27312734
onSpy.mockClear();
@@ -2812,7 +2815,7 @@ describe("createTelegramBot", () => {
28122815
expect(entry?.modelOverride).toBeUndefined();
28132816
const confirmation = String(firstEditMessageTextArg(2));
28142817
expect(confirmation).toBe(
2815-
`${CHECK_MARK_EMOJI} Model reset to default\n\nSession model selection cleared. ${outcomeText} Runtime unchanged. New replies use the agent's configured default.`,
2818+
`${CHECK_MARK_EMOJI} Model reset to default\n\nSession model selection cleared. ${outcomeText} Runtime set to <b>${expectedRuntime}</b> from configured policy. New replies use the agent's configured default.`,
28162819
);
28172820
expect(confirmation).not.toContain("team:prod");
28182821
}
@@ -2856,6 +2859,9 @@ describe("createTelegramBot", () => {
28562859

28572860
expect(replySpy).not.toHaveBeenCalled();
28582861
expect(editMessageTextSpy).toHaveBeenCalledTimes(1);
2862+
expect(firstEditMessageTextArg(2)).toContain(
2863+
"Selecting a model also applies its configured runtime.",
2864+
);
28592865
const params = firstEditMessageTextArg(3);
28602866
const inlineKeyboard = (
28612867
params as {
@@ -2875,7 +2881,36 @@ describe("createTelegramBot", () => {
28752881

28762882
it("formats non-default model selection confirmations with Telegram HTML parse mode", async () => {
28772883
const storePath = createTelegramTestStorePath("model-html");
2878-
const config = makeModelPickerConfig(storePath);
2884+
const config = makeModelPickerConfig(storePath, {
2885+
models: {
2886+
"anthropic/claude-opus-4-6": {},
2887+
"openai/gpt-5.4": { agentRuntime: { id: "openclaw" } },
2888+
},
2889+
});
2890+
const route = resolveTelegramConversationRoute({
2891+
cfg: config,
2892+
accountId: "default",
2893+
chatId: 1234,
2894+
isGroup: false,
2895+
threadSpec: { scope: "none" },
2896+
senderId: 9,
2897+
}).route;
2898+
const sessionKey = resolveTelegramConversationBaseSessionKey({
2899+
cfg: config,
2900+
route,
2901+
chatId: 1234,
2902+
isGroup: false,
2903+
senderId: 9,
2904+
});
2905+
await upsertSessionEntry({
2906+
storePath,
2907+
sessionKey,
2908+
entry: {
2909+
sessionId: "model-html",
2910+
updatedAt: 1,
2911+
agentRuntimeOverride: "openclaw",
2912+
},
2913+
});
28792914

28802915
loadConfig.mockReturnValue(config);
28812916
createTelegramBot({
@@ -2898,14 +2933,15 @@ describe("createTelegramBot", () => {
28982933
expect(editCall[0]).toBe(1234);
28992934
expect(editCall[1]).toBe(17);
29002935
expect(editCall[2]).toBe(
2901-
`${CHECK_MARK_EMOJI} Model changed to <b>openai/gpt-5.4</b>\n\nSession-only model selection. Runtime unchanged. Use /model openai/gpt-5.4 --runtime &lt;runtime&gt; -s to switch harnesses. The agent default in openclaw.json is unchanged. This chat keeps the model selection across /new and /reset; use /model default -s to clear the session model selection.`,
2936+
`${CHECK_MARK_EMOJI} Model changed to <b>openai/gpt-5.4</b>\n\nSession-only model selection. Runtime set to <b>openclaw</b> from configured policy. The agent default in openclaw.json is unchanged. This chat keeps the model selection across /new and /reset; use /model default -s to clear the session model selection.`,
29022937
);
29032938
expect(requireRecord(editCall[3], "edit params").parse_mode).toBe("HTML");
29042939

29052940
const entry = readOnlySessionEntry(storePath);
29062941
expect(entry?.providerOverride).toBe("openai");
29072942
expect(entry?.modelOverride).toBe("gpt-5.4");
29082943
expect(entry?.modelOverrideSource).toBe("user");
2944+
expect(entry?.agentRuntimeOverride).toBeUndefined();
29092945
expect(answerCallbackQuerySpy).toHaveBeenCalledWith("cbq-model-html-1");
29102946
});
29112947

‎src/model-picker/apply-session-model-selection.test.ts‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -574,50 +574,59 @@ describe("applySessionModelSelection", () => {
574574
runtime: { kind: "set", runtime: "openclaw" } as const,
575575
expected: "openclaw",
576576
runtimeChange: { kind: "set", runtime: "openclaw" },
577+
agentRuntime: "openclaw",
577578
},
578579
{
579580
name: "set idempotently",
580581
initial: "openclaw",
581582
runtime: { kind: "set", runtime: "openclaw" } as const,
582583
expected: "openclaw",
583584
runtimeChange: { kind: "set", runtime: "openclaw" },
585+
agentRuntime: "openclaw",
584586
},
585587
{
586588
name: "clear",
587589
initial: "openclaw",
588590
runtime: { kind: "clear" } as const,
589591
expected: undefined,
590592
runtimeChange: { kind: "clear" },
593+
agentRuntime: "codex",
591594
},
592595
{
593596
name: "clear idempotently",
594597
initial: undefined,
595598
runtime: { kind: "clear" } as const,
596599
expected: undefined,
597600
runtimeChange: { kind: "clear" },
601+
agentRuntime: "codex",
598602
},
599603
{
600604
name: "unchanged",
601605
initial: "openclaw",
602606
runtime: { kind: "unchanged" } as const,
603607
expected: "openclaw",
604608
runtimeChange: undefined,
609+
agentRuntime: "openclaw",
605610
},
606-
])("supports runtime $name", async ({ initial, runtime, expected, runtimeChange }) => {
607-
const sessionEntry = createEntry({ agentRuntimeOverride: initial });
608-
const result = await applySessionModelSelection(
609-
createParams({
610-
sessionEntry,
611-
request: { provider: "openai", model: "gpt-4o", isDefault: false, runtime },
612-
}),
613-
);
611+
])(
612+
"supports runtime $name",
613+
async ({ initial, runtime, expected, runtimeChange, agentRuntime }) => {
614+
const sessionEntry = createEntry({ agentRuntimeOverride: initial });
615+
const result = await applySessionModelSelection(
616+
createParams({
617+
sessionEntry,
618+
request: { provider: "openai", model: "gpt-4o", isDefault: false, runtime },
619+
}),
620+
);
614621

615-
expect(result.status).toBe("applied");
616-
if (result.status === "applied") {
617-
expect(result.runtimeChange).toEqual(runtimeChange);
618-
}
619-
expect(sessionEntry.agentRuntimeOverride).toBe(expected);
620-
});
622+
expect(result.status).toBe("applied");
623+
if (result.status === "applied") {
624+
expect(result.runtimeChange).toEqual(runtimeChange);
625+
expect(result.agentRuntime).toBe(agentRuntime);
626+
}
627+
expect(sessionEntry.agentRuntimeOverride).toBe(expected);
628+
},
629+
);
621630

622631
it("rejects an incompatible runtime without mutation or side effects", async () => {
623632
const sessionEntry = createEntry();

‎src/model-picker/apply-session-model-selection.ts‎

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export type ApplySessionModelSelectionResult =
7676
provider: string;
7777
model: string;
7878
effectiveModelRef: string;
79+
agentRuntime: string;
7980
changed: boolean;
8081
contextTokens: number;
8182
configuredDefaultUpdate?: StickyModelSelectionDispatchOutcome;
@@ -300,6 +301,17 @@ export async function applySessionModelSelection(
300301
persistedEntry = params.sessionEntry;
301302
}
302303

304+
const agentRuntime = resolveEffectiveAgentRuntime({
305+
cfg: params.cfg,
306+
provider: request.provider,
307+
modelId: request.model,
308+
modelApi: selectedCatalogEntry?.api,
309+
modelBaseUrl: selectedCatalogEntry?.baseUrl,
310+
agentId: params.agentId,
311+
sessionKey: params.sessionKey,
312+
sessionEntry: persistedEntry,
313+
});
314+
303315
const provider = request.provider;
304316
const model = request.model;
305317
const effectiveModelRef = `${provider}/${model}`;
@@ -338,16 +350,7 @@ export async function applySessionModelSelection(
338350
nextThinking: {
339351
level: persistedEntry.thinkingLevel,
340352
catalog: [...thinkingCatalog],
341-
agentRuntime: resolveEffectiveAgentRuntime({
342-
cfg: params.cfg,
343-
provider,
344-
modelId: model,
345-
modelApi: selectedCatalogEntry?.api,
346-
modelBaseUrl: selectedCatalogEntry?.baseUrl,
347-
agentId: params.agentId,
348-
sessionKey: params.sessionKey,
349-
sessionEntry: persistedEntry,
350-
}),
353+
agentRuntime,
351354
},
352355
});
353356
}
@@ -361,23 +364,15 @@ export async function applySessionModelSelection(
361364

362365
const contextProvider = resolveContextConfigProviderForRuntime({
363366
provider,
364-
runtimeId: resolveEffectiveAgentRuntime({
365-
cfg: params.cfg,
366-
provider,
367-
modelId: model,
368-
modelApi: selectedCatalogEntry?.api,
369-
modelBaseUrl: selectedCatalogEntry?.baseUrl,
370-
agentId: params.agentId,
371-
sessionKey: params.sessionKey,
372-
sessionEntry: persistedEntry,
373-
}),
367+
runtimeId: agentRuntime,
374368
config: params.cfg,
375369
});
376370
return {
377371
status: "applied",
378372
provider,
379373
model,
380374
effectiveModelRef,
375+
agentRuntime,
381376
changed,
382377
contextTokens: resolveContextTokens({
383378
cfg: params.cfg,

0 commit comments

Comments
 (0)