Skip to content

Commit 9a29304

Browse files
authored
fix(agents): avoid provider discovery when cache pruning is off (#117396)
1 parent 8738fe1 commit 9a29304

2 files changed

Lines changed: 74 additions & 7 deletions

File tree

‎src/agents/embedded-agent-runner/run/attempt-context-guards.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,61 @@ describe("installEmbeddedAttemptContextGuards", () => {
149149
expect(removeLoopHook).toHaveBeenCalledOnce();
150150
});
151151

152+
it.each([
153+
{ name: "attempt configuration is absent", config: undefined },
154+
{
155+
name: "context pruning is not configured",
156+
config: { agents: { defaults: {} } },
157+
},
158+
{
159+
name: "context pruning has no mode",
160+
config: { agents: { defaults: { contextPruning: {} } } },
161+
},
162+
{
163+
name: "context pruning is explicitly off",
164+
config: { agents: { defaults: { contextPruning: { mode: "off" } } } },
165+
},
166+
])("does not inspect providers when $name", ({ config }) => {
167+
hoisted.isCacheTtlEligibleProvider.mockReturnValue(true);
168+
const input = createInput();
169+
input.attempt = { ...input.attempt, config: config as never };
170+
171+
const guards = installEmbeddedAttemptContextGuards(input as never);
172+
173+
expect(hoisted.isCacheTtlEligibleProvider).not.toHaveBeenCalled();
174+
expect(hoisted.readLastCacheTtlTimestamp).not.toHaveBeenCalled();
175+
guards.remove();
176+
});
177+
178+
it("does not install cache-TTL pruning for an ineligible provider", async () => {
179+
const input = createInput();
180+
input.attempt = {
181+
...input.attempt,
182+
config: { agents: { defaults: { contextPruning: { mode: "cache-ttl" } } } } as never,
183+
};
184+
const originalTransform = vi.fn(async (messages: AgentMessage[]) => messages);
185+
input.activeSession.agent.transformContext = originalTransform;
186+
187+
const guards = installEmbeddedAttemptContextGuards(input as never);
188+
189+
expect(hoisted.isCacheTtlEligibleProvider).toHaveBeenCalledExactlyOnceWith(
190+
"provider-1",
191+
"model-1",
192+
"anthropic-messages",
193+
);
194+
expect(hoisted.readLastCacheTtlTimestamp).not.toHaveBeenCalled();
195+
const messages: AgentMessage[] = [
196+
{ role: "user", content: [{ type: "text", text: "hello" }], timestamp: 1 },
197+
];
198+
expect(
199+
await input.activeSession.agent.transformContext?.(messages, new AbortController().signal),
200+
).toBe(messages);
201+
expect(originalTransform).toHaveBeenCalledOnce();
202+
203+
guards.remove();
204+
expect(input.activeSession.agent.transformContext).toBe(originalTransform);
205+
});
206+
152207
it("projects expired cache-TTL history before context-engine assembly once per attempt", async () => {
153208
const now = Date.now();
154209
hoisted.isCacheTtlEligibleProvider.mockReturnValue(true);
@@ -193,6 +248,18 @@ describe("installEmbeddedAttemptContextGuards", () => {
193248
] as AgentMessage[];
194249

195250
const guards = installEmbeddedAttemptContextGuards(input as never);
251+
expect(hoisted.isCacheTtlEligibleProvider).toHaveBeenCalledExactlyOnceWith(
252+
"anthropic",
253+
"claude-sonnet-4-6",
254+
"anthropic-messages",
255+
);
256+
expect(hoisted.readLastCacheTtlTimestamp).toHaveBeenCalledExactlyOnceWith(
257+
input.sessionManager,
258+
{
259+
provider: "anthropic",
260+
modelId: "claude-sonnet-4-6",
261+
},
262+
);
196263
await input.activeSession.agent.transformContext?.(messages, new AbortController().signal);
197264
const firstTool = engineMessages?.find((message) => message.role === "toolResult");
198265
expect(firstTool?.content[0]).toMatchObject({

‎src/agents/embedded-agent-runner/run/attempt-context-guards.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ export function installEmbeddedAttemptContextGuards(input: {
8686
}
8787
: {};
8888

89-
const cacheTtlSettings = isCacheTtlEligibleProvider(
90-
attempt.provider,
91-
attempt.modelId,
92-
attempt.model.api,
93-
)
94-
? resolveCacheTtlPruningSettings(attempt.config?.agents?.defaults?.contextPruning)
95-
: undefined;
89+
const contextPruning = attempt.config?.agents?.defaults?.contextPruning;
90+
// Disabled pruning must not resolve provider hooks and cold-load plugin metadata.
91+
const cacheTtlSettings =
92+
contextPruning?.mode === "cache-ttl" &&
93+
isCacheTtlEligibleProvider(attempt.provider, attempt.modelId, attempt.model.api)
94+
? resolveCacheTtlPruningSettings(contextPruning)
95+
: undefined;
9696
const previousCacheTtlTransform = activeSession.agent.transformContext;
9797
let lastCacheTouchAt = cacheTtlSettings
9898
? readLastCacheTtlTimestamp(input.sessionManager, {

0 commit comments

Comments
 (0)