@@ -58,21 +58,15 @@ def __init__(self, window_size: int = 40, should_truncate_results: bool = True):
58
58
def apply_management (self , agent : "Agent" , ** kwargs : Any ) -> None :
59
59
"""Apply the sliding window to the agent's messages array to maintain a manageable history size.
60
60
61
- This method is called after every event loop cycle, as the messages array may have been modified with tool
62
- results and assistant responses. It first removes any dangling messages that might create an invalid
63
- conversation state, then applies the sliding window if the message count exceeds the window size.
64
-
65
- Special handling is implemented to ensure we don't leave a user message with toolResult
66
- as the first message in the array. It also ensures that all toolUse blocks have corresponding toolResult
67
- blocks to maintain conversation coherence.
61
+ This method is called after every event loop cycle to apply a sliding window if the message count
62
+ exceeds the window size.
68
63
69
64
Args:
70
65
agent: The agent whose messages will be managed.
71
66
This list is modified in-place.
72
67
**kwargs: Additional keyword arguments for future extensibility.
73
68
"""
74
69
messages = agent .messages
75
- self ._remove_dangling_messages (messages )
76
70
77
71
if len (messages ) <= self .window_size :
78
72
logger .debug (
@@ -81,37 +75,6 @@ def apply_management(self, agent: "Agent", **kwargs: Any) -> None:
81
75
return
82
76
self .reduce_context (agent )
83
77
84
- def _remove_dangling_messages (self , messages : Messages ) -> None :
85
- """Remove dangling messages that would create an invalid conversation state.
86
-
87
- After the event loop cycle is executed, we expect the messages array to end with either an assistant tool use
88
- request followed by the pairing user tool result or an assistant response with no tool use request. If the
89
- event loop cycle fails, we may end up in an invalid message state, and so this method will remove problematic
90
- messages from the end of the array.
91
-
92
- This method handles two specific cases:
93
-
94
- - User with no tool result: Indicates that event loop failed to generate an assistant tool use request
95
- - Assistant with tool use request: Indicates that event loop failed to generate a pairing user tool result
96
-
97
- Args:
98
- messages: The messages to clean up.
99
- This list is modified in-place.
100
- """
101
- # remove any dangling user messages with no ToolResult
102
- if len (messages ) > 0 and is_user_message (messages [- 1 ]):
103
- if not any ("toolResult" in content for content in messages [- 1 ]["content" ]):
104
- messages .pop ()
105
-
106
- # remove any dangling assistant messages with ToolUse
107
- if len (messages ) > 0 and is_assistant_message (messages [- 1 ]):
108
- if any ("toolUse" in content for content in messages [- 1 ]["content" ]):
109
- messages .pop ()
110
- # remove remaining dangling user messages with no ToolResult after we popped off an assistant message
111
- if len (messages ) > 0 and is_user_message (messages [- 1 ]):
112
- if not any ("toolResult" in content for content in messages [- 1 ]["content" ]):
113
- messages .pop ()
114
-
115
78
def reduce_context (self , agent : "Agent" , e : Optional [Exception ] = None , ** kwargs : Any ) -> None :
116
79
"""Trim the oldest messages to reduce the conversation context size.
117
80
0 commit comments