Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/coding-agent/src/core/agent-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2231,6 +2231,16 @@ export class AgentSession {
});
}

/**
* Rebuild the cached base system prompt so the # Environment block reflects the
* current model. Model switches update agent.state.model but the prompt is
* otherwise only rebuilt at init, so without this the model reports a stale id.
*/
private _refreshSystemPromptForModel(): void {
this._baseSystemPrompt = this._rebuildSystemPrompt(this.getActiveToolNames());
this.agent.state.systemPrompt = this._baseSystemPrompt;
}

/**
* Set model directly.
* Validates that auth is configured, saves to session and settings.
Expand All @@ -2250,6 +2260,8 @@ export class AgentSession {
// Re-clamp thinking level for new model's capabilities
this.setThinkingLevel(thinkingLevel);

this._refreshSystemPromptForModel();

await this._emitModelSelect(model, previousModel, "set");
}

Expand Down Expand Up @@ -2290,6 +2302,8 @@ export class AgentSession {
// setThinkingLevel clamps to model capabilities.
this.setThinkingLevel(thinkingLevel);

this._refreshSystemPromptForModel();

await this._emitModelSelect(next.model, currentModel, "cycle");

return { model: next.model, thinkingLevel: this.thinkingLevel, isScoped: true };
Expand All @@ -2315,6 +2329,8 @@ export class AgentSession {
// Re-clamp thinking level for new model's capabilities
this.setThinkingLevel(thinkingLevel);

this._refreshSystemPromptForModel();

await this._emitModelSelect(nextModel, currentModel, "cycle");

return { model: nextModel, thinkingLevel: this.thinkingLevel, isScoped: false };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@ describe("AgentSession model and extension characterization", () => {
).toEqual([`${nextModel.provider}/${nextModel.id}`]);
});

it("setModel refreshes the system prompt so the provider sees the new model", async () => {
const harness = await createHarness({
models: [
{ id: "faux-1", name: "One", reasoning: true },
{ id: "faux-2", name: "Two", reasoning: true },
],
});
harnesses.push(harness);

const seenSystemPrompts: string[] = [];
const capture = (context: { systemPrompt?: string }) => {
seenSystemPrompts.push(context.systemPrompt ?? "");
return fauxAssistantMessage("ok");
};

harness.setResponses([capture]);
await harness.session.prompt("first");

await harness.session.setModel(harness.getModel("faux-2")!);

harness.appendResponses([capture]);
await harness.session.prompt("second");

expect(seenSystemPrompts[0]).toContain("Active model: faux-1");
expect(seenSystemPrompts[1]).toContain("Active model: faux-2");
expect(seenSystemPrompts[1]).not.toContain("Active model: faux-1");
});

it("cycles through scoped models and preserves the scoped thinking preference", async () => {
const harness = await createHarness({
models: [
Expand Down
Loading