Skip to content

Commit 3d34ad0

Browse files
committed
10 minutes before cache clears in free mode
1 parent 3a76beb commit 3d34ad0

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

agents/base2/base2.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -284,22 +284,40 @@ ${PLACEHOLDER.GIT_CHANGES_PROMPT}
284284
noAskUser,
285285
}),
286286

287-
handleSteps: function* ({ params }) {
288-
while (true) {
289-
// Run context-pruner before each step
290-
yield {
291-
toolName: 'spawn_agent_inline',
292-
input: {
293-
agent_type: 'context-pruner',
294-
params: params ?? {},
295-
},
296-
includeToolCall: false,
297-
} as any
298-
299-
const { stepsComplete } = yield 'STEP'
300-
if (stepsComplete) break
301-
}
302-
},
287+
// handleSteps is serialized via .toString() and re-eval'd, so closure
288+
// variables like `isFree` are not in scope at runtime. Pick the right
289+
// literal-baked function here instead.
290+
handleSteps: isFree
291+
? function* ({ params }) {
292+
while (true) {
293+
yield {
294+
toolName: 'spawn_agent_inline',
295+
input: {
296+
agent_type: 'context-pruner',
297+
params: { ...(params ?? {}), cacheExpiryMs: 10 * 60 * 1000 },
298+
},
299+
includeToolCall: false,
300+
} as any
301+
302+
const { stepsComplete } = yield 'STEP'
303+
if (stepsComplete) break
304+
}
305+
}
306+
: function* ({ params }) {
307+
while (true) {
308+
yield {
309+
toolName: 'spawn_agent_inline',
310+
input: {
311+
agent_type: 'context-pruner',
312+
params: params ?? {},
313+
},
314+
includeToolCall: false,
315+
} as any
316+
317+
const { stepsComplete } = yield 'STEP'
318+
if (stepsComplete) break
319+
}
320+
},
303321
}
304322
}
305323

agents/context-pruner.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const definition: AgentDefinition = {
3131
userBudget: {
3232
type: 'number',
3333
},
34+
cacheExpiryMs: {
35+
type: 'number',
36+
},
3437
},
3538
required: [],
3639
},
@@ -74,8 +77,8 @@ const definition: AgentDefinition = {
7477
/** Fudge factor for token count threshold to trigger pruning earlier */
7578
const TOKEN_COUNT_FUDGE_FACTOR = 1_000
7679

77-
/** Prompt cache expiry time (Anthropic caches for 5 minutes) */
78-
const CACHE_EXPIRY_MS = 5 * 60 * 1000
80+
/** Prompt cache expiry time (Anthropic caches for 5 minutes by default) */
81+
const CACHE_EXPIRY_MS: number = params?.cacheExpiryMs ?? 5 * 60 * 1000
7982

8083
/** Header used in conversation summaries */
8184
const SUMMARY_HEADER =
@@ -328,6 +331,17 @@ const definition: AgentDefinition = {
328331
currentMessages.splice(lastSubagentSpawnIndex, 1)
329332
}
330333

334+
// Also remove the params USER_PROMPT if params were provided to this agent
335+
// (this is the message like <user_message>{"cacheExpiryMs": 600000}</user_message>)
336+
if (params && Object.keys(params).length > 0) {
337+
const lastUserPromptIndex = currentMessages.findLastIndex((message) =>
338+
message.tags?.includes('USER_PROMPT'),
339+
)
340+
if (lastUserPromptIndex !== -1) {
341+
currentMessages.splice(lastUserPromptIndex, 1)
342+
}
343+
}
344+
331345
// Check for prompt cache miss (>5 min gap before the USER_PROMPT message)
332346
// The USER_PROMPT is the actual user message; INSTRUCTIONS_PROMPT comes after it
333347
// We need to find the USER_PROMPT and check the gap between it and the last assistant message

0 commit comments

Comments
 (0)