Skip to content

Commit c317f38

Browse files
committed
Add prune cooldown to prevent immediate re-pruning
When the last tool used was prune, inject a cooldown message instead of the full prunable-tools list. This prevents the model from repeatedly invoking the prune tool in successive turns. Also refactors magic strings into named constants for better maintainability.
1 parent ed2d32d commit c317f38

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

lib/messages/prune.ts

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ const PRUNED_TOOL_INPUT_REPLACEMENT = '[Input removed to save context]'
1010
const PRUNED_TOOL_OUTPUT_REPLACEMENT = '[Output removed to save context - information superseded or no longer needed]'
1111
const NUDGE_STRING = loadPrompt("nudge")
1212

13+
const wrapPrunableTools = (content: string): string => `<prunable-tools>
14+
The following tools have been invoked and are available for pruning. This list does not mandate immediate action. Consider your current goals and the resources you need before discarding valuable tool inputs or outputs. Keep the context free of noise.
15+
${content}
16+
</prunable-tools>`
17+
const PRUNABLE_TOOLS_COOLDOWN = `<prunable-tools>
18+
Pruning was just performed. Do not use the prune tool again. A fresh list will be available after your next tool use.
19+
</prunable-tools>`
20+
21+
const SYNTHETIC_MESSAGE_ID = "msg_01234567890123456789012345"
22+
const SYNTHETIC_PART_ID = "prt_01234567890123456789012345"
23+
1324
const buildPrunableToolsList = (
1425
state: SessionState,
1526
config: PluginConfig,
@@ -41,7 +52,7 @@ const buildPrunableToolsList = (
4152
return ""
4253
}
4354

44-
return `<prunable-tools>\nThe following tools have been invoked and are available for pruning. This list does not mandate immediate action. Consider your current goals and the resources you need before discarding valuable tool inputs or outputs. Keep the context free of noise.\n${lines.join('\n')}\n</prunable-tools>`
55+
return wrapPrunableTools(lines.join('\n'))
4556
}
4657

4758
export const insertPruneToolContext = (
@@ -59,20 +70,31 @@ export const insertPruneToolContext = (
5970
return
6071
}
6172

62-
const prunableToolsList = buildPrunableToolsList(state, config, logger, messages)
63-
if (!prunableToolsList) {
64-
return
65-
}
73+
let prunableToolsContent: string
74+
75+
if (state.lastToolPrune) {
76+
logger.debug("Last tool was prune - injecting cooldown message")
77+
prunableToolsContent = PRUNABLE_TOOLS_COOLDOWN
78+
} else {
79+
const prunableToolsList = buildPrunableToolsList(state, config, logger, messages)
80+
if (!prunableToolsList) {
81+
return
82+
}
83+
84+
logger.debug("prunable-tools: \n" + prunableToolsList)
85+
86+
let nudgeString = ""
87+
if (state.nudgeCounter >= config.strategies.pruneTool.nudge.frequency) {
88+
logger.info("Inserting prune nudge message")
89+
nudgeString = "\n" + NUDGE_STRING
90+
}
6691

67-
let nudgeString = ""
68-
if (state.nudgeCounter >= config.strategies.pruneTool.nudge.frequency) {
69-
logger.info("Inserting prune nudge message")
70-
nudgeString = "\n" + NUDGE_STRING
92+
prunableToolsContent = prunableToolsList + nudgeString
7193
}
7294

7395
const userMessage: WithParts = {
7496
info: {
75-
id: "msg_01234567890123456789012345",
97+
id: SYNTHETIC_MESSAGE_ID,
7698
sessionID: lastUserMessage.info.sessionID,
7799
role: "user",
78100
time: { created: Date.now() },
@@ -84,11 +106,11 @@ export const insertPruneToolContext = (
84106
},
85107
parts: [
86108
{
87-
id: "prt_01234567890123456789012345",
109+
id: SYNTHETIC_PART_ID,
88110
sessionID: lastUserMessage.info.sessionID,
89-
messageID: "msg_01234567890123456789012345",
111+
messageID: SYNTHETIC_MESSAGE_ID,
90112
type: "text",
91-
text: prunableToolsList + nudgeString,
113+
text: prunableToolsContent,
92114
}
93115
]
94116
}

0 commit comments

Comments
 (0)