Skip to content

Commit fe42117

Browse files
committed
refactor(agents): address review feedback on resolveFreshModel
- Remove dead code: use resolveFreshModel()! in getAvailableThinkingLevels since the early return guarantees this.model is truthy - Add JSDoc explaining why resolveFreshModel() does not use refreshCurrentModelFromRegistry() (mutation vs transient read) - Add defensive test for registry returning different provider/id - Add integration test verifying compaction uses fresh contextWindow
1 parent f3e8872 commit fe42117

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/agents/sessions/agent-session.resolve-fresh-model.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,27 @@ describe("resolveFreshModel", () => {
9292
high: "3000",
9393
});
9494
});
95+
96+
it("returns registry model even when provider/id differ from snapshot", () => {
97+
const snapshot = makeModel({ provider: "openai", id: "gpt-4o" });
98+
const fresh = makeModel({ provider: "openai", id: "gpt-4o-mini" });
99+
const find = vi.fn().mockReturnValue(fresh);
100+
101+
const result = resolveFreshModel(snapshot, find);
102+
expect(result).toBe(fresh);
103+
expect(result?.id).toBe("gpt-4o-mini");
104+
});
105+
106+
it("uses fresh contextWindow for compaction threshold check", () => {
107+
const snapshot = makeModel({ contextWindow: 200_000 });
108+
const fresh = makeModel({ contextWindow: 1_000_000 });
109+
const find = vi.fn().mockReturnValue(fresh);
110+
111+
const resolved = resolveFreshModel(snapshot, find);
112+
const contextWindow = resolved?.contextWindow ?? 0;
113+
114+
const usageRatio = 180_000 / contextWindow;
115+
expect(contextWindow).toBe(1_000_000);
116+
expect(usageRatio).toBeLessThan(0.8);
117+
});
95118
});

src/agents/sessions/agent-session.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1722,7 +1722,7 @@ export class AgentSession {
17221722
if (!this.model) {
17231723
return THINKING_LEVELS;
17241724
}
1725-
return getSupportedThinkingLevels(this.resolveFreshModel() ?? this.model) as ThinkingLevel[];
1725+
return getSupportedThinkingLevels(this.resolveFreshModel()!) as ThinkingLevel[];
17261726
}
17271727

17281728
/**
@@ -2265,6 +2265,11 @@ export class AgentSession {
22652265
* Falls back to the snapshot if the registry has no entry for the current model.
22662266
* Ensures post-turn reads (contextWindow, reasoning, thinkingBudgets) reflect
22672267
* the latest registry state after a /model switch.
2268+
*
2269+
* Why not call refreshCurrentModelFromRegistry() instead?
2270+
* That method mutates agent.state.model — a persistent side effect.
2271+
* Post-turn reads should not change the snapshot mid-turn.
2272+
* This helper only does a transient read from the registry.
22682273
*/
22692274
private resolveFreshModel(): Model | undefined {
22702275
const current = this.model;

0 commit comments

Comments
 (0)