Skip to content

Commit fb119d8

Browse files
authored
fix(sessions): refresh model from registry on session restore (#502)
Persisted sessions store the full model object including contextWindow. After a dependency bump that updates registry metadata (e.g. Opus 4.6 going from 200k to 1M context), restored sessions would still show the old value because setModel() received the stale persisted object. Now re-resolve the model from getModel(provider, id) on restore, picking up the latest contextWindow, pricing, and other metadata. Falls back to the persisted model for custom-gateway models not in the registry.
1 parent 87058ac commit fb119d8

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/taskpane/sessions.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* - session identity lifecycle (new / rename / resume)
88
*/
99

10+
import type { Model } from "@mariozechner/pi-ai";
11+
import { getModel } from "@mariozechner/pi-ai";
1012
import type { Agent, AgentMessage } from "@mariozechner/pi-agent-core";
1113
import type { SessionData } from "@mariozechner/pi-web-ui/dist/storage/types.js";
1214
import type { SessionsStore } from "@mariozechner/pi-web-ui/dist/storage/stores/sessions-store.js";
@@ -42,6 +44,22 @@ type UserLikeMessage = AgentMessage & {
4244
content: unknown;
4345
};
4446

47+
/**
48+
* Re-resolve a persisted model against the current registry so that
49+
* metadata like `contextWindow` picks up upstream changes (e.g. a dep
50+
* bump that raised Opus 4.6 from 200k → 1M). Falls back to the
51+
* persisted model if the registry doesn't have it (custom gateways, etc.).
52+
*/
53+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Model<any> from session store
54+
function refreshModelFromRegistry(persisted: Model<any>): Model<any> {
55+
try {
56+
const fresh = getModel(persisted.provider as never, persisted.id as never);
57+
return fresh ?? persisted;
58+
} catch {
59+
return persisted;
60+
}
61+
}
62+
4563
function hasAssistantMessage(messages: AgentMessage[]): boolean {
4664
return messages.some((m) => m.role === "assistant");
4765
}
@@ -281,7 +299,7 @@ export async function setupSessionPersistence(opts: {
281299
agent.replaceMessages(sessionData.messages);
282300

283301
if (sessionData.model) {
284-
agent.setModel(sessionData.model);
302+
agent.setModel(refreshModelFromRegistry(sessionData.model));
285303
}
286304
if (sessionData.thinkingLevel) {
287305
agent.setThinkingLevel(sessionData.thinkingLevel);

0 commit comments

Comments
 (0)