@@ -76,7 +76,10 @@ import {
7676 normalizeMainKey ,
7777 parseAgentSessionKey ,
7878} from "../routing/session-key.js" ;
79- import { hasStaleAutoRuntimeAuthProfileSelection } from "../sessions/model-overrides.js" ;
79+ import {
80+ hasStaleAutoRuntimeAuthProfileSelection ,
81+ type StaleAutoRuntimeAuthProfileEntry ,
82+ } from "../sessions/model-overrides.js" ;
8083import { isCronRunSessionKey } from "../sessions/session-key-utils.js" ;
8184import {
8285 AVATAR_MAX_BYTES ,
@@ -141,6 +144,26 @@ export type {
141144 SessionsPreviewResult ,
142145} from "./session-utils.types.js" ;
143146
147+ type SessionRuntimeModelEntry = Pick <
148+ SessionEntry ,
149+ "model" | "modelProvider" | "modelOverride" | "providerOverride"
150+ > ;
151+
152+ type SessionChannelModelSelectionEntry = Pick <
153+ SessionEntry ,
154+ | "channel"
155+ | "origin"
156+ | "lastChannel"
157+ | "groupId"
158+ | "chatType"
159+ | "groupChannel"
160+ | "subject"
161+ | "parentSessionKey"
162+ > ;
163+
164+ export type SessionExpectedModelEntry = StaleAutoRuntimeAuthProfileEntry &
165+ SessionChannelModelSelectionEntry ;
166+
144167const DERIVED_TITLE_MAX_LEN = 60 ;
145168
146169function tryResolveExistingPath ( value : string ) : string | null {
@@ -767,31 +790,14 @@ function resolveSessionSelectedModelRef(params: {
767790 return selected ;
768791}
769792
770- function resolveStaleAutoRuntimeExpectedModelRef ( params : {
793+ // Expected selection ignores transient runtime fields; callers use it to decide
794+ // whether auto-owned runtime/auth state should be repaired or inherited.
795+ export function resolveSessionExpectedSelectedModelRef ( params : {
771796 cfg : OpenClawConfig ;
772- entry ?:
773- | SessionEntry
774- | Pick <
775- SessionEntry ,
776- | "authProfileOverride"
777- | "authProfileOverrideCompactionCount"
778- | "authProfileOverrideSource"
779- | "providerOverride"
780- | "modelOverride"
781- | "modelProvider"
782- | "model"
783- | "channel"
784- | "origin"
785- | "lastChannel"
786- | "groupId"
787- | "chatType"
788- | "groupChannel"
789- | "subject"
790- | "parentSessionKey"
791- > ;
797+ entry ?: SessionExpectedModelEntry ;
792798 agentId ?: string ;
793799 allowPluginNormalization ?: boolean ;
794- } ) : ReturnType < typeof resolveSessionModelRef > | null {
800+ } ) : { provider : string ; model : string } {
795801 const defaultSelection = params . agentId
796802 ? resolveDefaultModelForAgent ( {
797803 cfg : params . cfg ,
@@ -804,6 +810,23 @@ function resolveStaleAutoRuntimeExpectedModelRef(params: {
804810 defaultModel : DEFAULT_MODEL ,
805811 allowPluginNormalization : params . allowPluginNormalization ,
806812 } ) ;
813+
814+ const normalizedOverride = normalizeStoredOverrideModel ( {
815+ providerOverride : params . entry ?. providerOverride ,
816+ modelOverride : params . entry ?. modelOverride ,
817+ } ) ;
818+ if ( normalizedOverride . modelOverride ) {
819+ return resolveSessionModelRef (
820+ params . cfg ,
821+ {
822+ providerOverride : normalizedOverride . providerOverride ,
823+ modelOverride : normalizedOverride . modelOverride ,
824+ } ,
825+ params . agentId ,
826+ { allowPluginNormalization : params . allowPluginNormalization } ,
827+ ) ;
828+ }
829+
807830 const channelModelOverride = params . entry
808831 ? resolveChannelModelOverride ( {
809832 cfg : params . cfg ,
@@ -823,7 +846,16 @@ function resolveStaleAutoRuntimeExpectedModelRef(params: {
823846 allowPluginNormalization : params . allowPluginNormalization ,
824847 } ) ?. ref
825848 : null ;
826- const expected = channelSelection ?? defaultSelection ;
849+ return channelSelection ?? defaultSelection ;
850+ }
851+
852+ function resolveStaleAutoRuntimeExpectedModelRef ( params : {
853+ cfg : OpenClawConfig ;
854+ entry ?: SessionExpectedModelEntry ;
855+ agentId ?: string ;
856+ allowPluginNormalization ?: boolean ;
857+ } ) : ReturnType < typeof resolveSessionModelRef > | null {
858+ const expected = resolveSessionExpectedSelectedModelRef ( params ) ;
827859 return hasStaleAutoRuntimeAuthProfileSelection ( params . entry , {
828860 ...expected ,
829861 config : params . cfg ,
@@ -1674,9 +1706,7 @@ export function getSessionDefaults(
16741706
16751707export function resolveSessionModelRef (
16761708 cfg : OpenClawConfig ,
1677- entry ?:
1678- | SessionEntry
1679- | Pick < SessionEntry , "model" | "modelProvider" | "modelOverride" | "providerOverride" > ,
1709+ entry ?: SessionRuntimeModelEntry ,
16801710 agentId ?: string ,
16811711 options ?: { allowPluginNormalization ?: boolean } ,
16821712) : { provider : string ; model : string } {
@@ -1726,29 +1756,12 @@ export function resolveSessionModelRef(
17261756
17271757export function resolveSessionNextRunModelRef (
17281758 cfg : OpenClawConfig ,
1729- entry ?:
1730- | SessionEntry
1731- | Pick <
1732- SessionEntry ,
1733- | "authProfileOverride"
1734- | "authProfileOverrideCompactionCount"
1735- | "authProfileOverrideSource"
1736- | "providerOverride"
1737- | "modelOverride"
1738- | "modelProvider"
1739- | "model"
1740- | "channel"
1741- | "origin"
1742- | "lastChannel"
1743- | "groupId"
1744- | "chatType"
1745- | "groupChannel"
1746- | "subject"
1747- | "parentSessionKey"
1748- > ,
1759+ entry ?: SessionExpectedModelEntry ,
17491760 agentId ?: string ,
17501761 options ?: { allowPluginNormalization ?: boolean } ,
17511762) : { provider : string ; model : string } {
1763+ // Dispatch/preflight wants the model the next run should use; generic row and
1764+ // history callers may still need the stored runtime model exactly as recorded.
17521765 const staleRuntimeExpected = resolveStaleAutoRuntimeExpectedModelRef ( {
17531766 cfg,
17541767 entry,
@@ -1838,18 +1851,7 @@ export async function resolveGatewayModelSupportsImages(params: {
18381851
18391852export function resolveSessionModelIdentityRef (
18401853 cfg : OpenClawConfig ,
1841- entry ?:
1842- | SessionEntry
1843- | Pick <
1844- SessionEntry ,
1845- | "model"
1846- | "modelProvider"
1847- | "modelOverride"
1848- | "providerOverride"
1849- | "authProfileOverride"
1850- | "authProfileOverrideSource"
1851- | "authProfileOverrideCompactionCount"
1852- > ,
1854+ entry ?: SessionExpectedModelEntry ,
18531855 agentId ?: string ,
18541856 fallbackModelRef ?: string ,
18551857 options ?: { allowPluginNormalization ?: boolean } ,
0 commit comments