Repository navigation
Expand file tree
/
Copy pathreflect_retry_tool_plugin.py
More file actions
369 lines (309 loc) · 12.7 KB
/
Copy pathreflect_retry_tool_plugin.py
File metadata and controls
369 lines (309 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import asyncio
import json
from typing import Any
from typing import Optional
from pydantic import BaseModel
from ..tools.base_tool import BaseTool
from ..tools.tool_context import ToolContext
from ._reflect_retry_utils import GLOBAL_SCOPE_KEY as GLOBAL_SCOPE_KEY
from ._reflect_retry_utils import REFLECT_AND_RETRY_RESPONSE_TYPE
from ._reflect_retry_utils import resolve_scope_key
from ._reflect_retry_utils import ScopedFailureTracker
from ._reflect_retry_utils import TrackingScope
from .base_plugin import BasePlugin
# A mapping from a tool's name to its consecutive failure count.
PerToolFailuresCounter = dict[str, int]
class ToolFailureResponse(BaseModel):
"""Response containing tool failure details and retry guidance."""
response_type: str = REFLECT_AND_RETRY_RESPONSE_TYPE
error_type: str = ""
error_details: str = ""
retry_count: int = 0
reflection_guidance: str = ""
class ReflectAndRetryToolPlugin(BasePlugin):
"""Provides self-healing, concurrent-safe error recovery for tool failures.
This plugin intercepts tool failures, provides structured guidance to the LLM
for reflection and correction, and retries the operation up to a configurable
limit.
**Key Features:**
- **Concurrency Safe:** Uses locking to safely handle parallel tool
executions
- **Configurable Scope:** Tracks failures per-invocation (default) or globally
using the `TrackingScope` enum.
- **Extensible Scoping:** The `_get_scope_key` method can be overridden to
implement custom tracking logic (e.g., per-user or per-session).
- **Granular Tracking:** Failure counts are tracked per-tool within the
defined scope. A success with one tool resets its counter without affecting
others.
- **Custom Error Extraction:** Supports detecting errors in normal tool
responses
that
don't throw exceptions, by overriding the `extract_error_from_result`
method.
**Example:**
```python
from my_project.plugins import ReflectAndRetryToolPlugin, TrackingScope
# Example 1: (MOST COMMON USAGE):
# Track failures only within the current agent invocation (default).
error_handling_plugin = ReflectAndRetryToolPlugin(max_retries=3)
# Example 2:
# Track failures globally across all turns and users.
global_error_handling_plugin = ReflectAndRetryToolPlugin(max_retries=5,
scope=TrackingScope.GLOBAL)
# Example 3:
# Retry on failures but do not throw exceptions.
error_handling_plugin =
ReflectAndRetryToolPlugin(max_retries=3,
throw_exception_if_retry_exceeded=False)
# Example 4:
# Track failures in successful tool responses that contain errors.
class CustomRetryPlugin(ReflectAndRetryToolPlugin):
async def extract_error_from_result(self, *, tool, tool_args,tool_context,
result):
# Detect error based on response content
if result.get('status') == 'error':
return result
return None # No error detected
error_handling_plugin = CustomRetryPlugin(max_retries=5)
```
"""
def __init__(
self,
name: str = "reflect_retry_tool_plugin",
max_retries: int = 3,
throw_exception_if_retry_exceeded: bool = True,
tracking_scope: TrackingScope = TrackingScope.INVOCATION,
):
"""Initializes the ReflectAndRetryToolPlugin.
Args:
name: Plugin instance identifier.
max_retries: Maximum consecutive failures before giving up (0 = no
retries).
throw_exception_if_retry_exceeded: If True, raises the final exception
when the retry limit is reached. If False, returns guidance instead.
tracking_scope: Determines the lifecycle of the error tracking state.
Defaults to `TrackingScope.INVOCATION` tracking per-invocation.
"""
super().__init__(name)
if max_retries < 0:
raise ValueError("max_retries must be a non-negative integer.")
self.max_retries = max_retries
self.throw_exception_if_retry_exceeded = throw_exception_if_retry_exceeded
self.scope = tracking_scope
self._tracker = ScopedFailureTracker()
self._scoped_failure_counters = self._tracker._scoped_failure_counters
self._lock = self._tracker._lock
async def after_tool_callback(
self,
*,
tool: BaseTool,
tool_args: dict[str, Any],
tool_context: ToolContext,
result: Any,
) -> Optional[dict[str, Any]]:
"""Handles successful tool calls or extracts and processes errors.
Args:
tool: The tool that was called.
tool_args: The arguments passed to the tool.
tool_context: The context of the tool call.
result: The result of the tool call.
Returns:
An optional dictionary containing reflection guidance if an error is
detected, or None if the tool call was successful or the
response is already a reflection message.
"""
if (
isinstance(result, dict)
and result.get("response_type") == REFLECT_AND_RETRY_RESPONSE_TYPE
):
return None
error = await self.extract_error_from_result(
tool=tool, tool_args=tool_args, tool_context=tool_context, result=result
)
if error:
return await self._handle_tool_error(tool, tool_args, tool_context, error)
# On success, reset the failure count for this specific tool within
# its scope.
await self._reset_failures_for_tool(tool_context, tool.name)
return None
async def extract_error_from_result(
self,
*,
tool: BaseTool,
tool_args: dict[str, Any],
tool_context: ToolContext,
result: Any,
) -> Optional[dict[str, Any]]:
"""Extracts an error from a successful tool result and triggers retry logic.
This is useful when tool call finishes successfully but the result contains
an error object like {"error": ...} that should be handled by the plugin.
By overriding this method, you can trigger retry logic on these successful
results that contain errors.
Args:
tool: The tool that was called.
tool_args: The arguments passed to the tool.
tool_context: The context of the tool call.
result: The result of the tool call.
Returns:
The extracted error if any, or None if no error was detected.
"""
return None
async def on_tool_error_callback(
self,
*,
tool: BaseTool,
tool_args: dict[str, Any],
tool_context: ToolContext,
error: Exception,
) -> Optional[dict[str, Any]]:
"""Handles tool exceptions by providing reflection guidance.
Args:
tool: The tool that was called.
tool_args: The arguments passed to the tool.
tool_context: The context of the tool call.
error: The exception raised by the tool.
Returns:
An optional dictionary containing reflection guidance for the error.
"""
if isinstance(error, asyncio.CancelledError):
return None
return await self._handle_tool_error(tool, tool_args, tool_context, error)
async def _handle_tool_error(
self,
tool: BaseTool,
tool_args: dict[str, Any],
tool_context: ToolContext,
error: Any,
) -> Optional[dict[str, Any]]:
"""Central logic for processing tool errors.
Failure counts are updated under an asyncio lock, which serializes
coroutines on a single event loop but provides no mutual exclusion
between threads.
Args:
tool: The tool that was called.
tool_args: The arguments passed to the tool.
tool_context: The context of the tool call.
error: The error to be handled.
Returns:
An optional dictionary containing reflection guidance for the error.
"""
if self.max_retries == 0:
if self.throw_exception_if_retry_exceeded:
raise self._ensure_exception(error)
return self._get_tool_retry_exceed_msg(tool, tool_args, error)
scope_key = self._get_scope_key(tool_context)
current_retries = await self._tracker.increment(scope_key, tool.name)
if current_retries <= self.max_retries:
return self._create_tool_reflection_response(
tool, tool_args, error, current_retries
)
# Max Retry exceeded
if self.throw_exception_if_retry_exceeded:
raise self._ensure_exception(error)
else:
return self._get_tool_retry_exceed_msg(tool, tool_args, error)
def _get_scope_key(self, tool_context: ToolContext) -> str:
"""Returns a unique key for the state dictionary based on the scope.
This method can be overridden in a subclass to implement custom scoping
logic, for example, tracking failures on a per-user or per-session basis.
"""
return resolve_scope_key(self.scope, tool_context.invocation_id)
async def _reset_failures_for_tool(
self, tool_context: ToolContext, tool_name: str
) -> None:
"""Atomically resets the failure count for a tool and cleans up state."""
scope = self._get_scope_key(tool_context)
await self._tracker.reset(scope, tool_name)
def _ensure_exception(self, error: Any) -> Exception:
"""Ensures the given error is an Exception instance, wrapping if not."""
return error if isinstance(error, Exception) else Exception(str(error))
def _format_error_details(self, error: Any) -> str:
"""Formats error details for inclusion in the reflection message."""
if isinstance(error, Exception):
return f"{type(error).__name__}: {str(error)}"
return str(error)
def _create_tool_reflection_response(
self,
tool: BaseTool,
tool_args: dict[str, Any],
error: Any,
retry_count: int,
) -> dict[str, Any]:
"""Generates structured reflection guidance for tool failures."""
args_summary = json.dumps(tool_args, indent=2, default=str)
error_details = self._format_error_details(error)
reflection_message = f"""
The call to tool `{tool.name}` failed.
**Error Details:**
```
{error_details}
```
**Tool Arguments Used:**
```json
{args_summary}
```
**Reflection Guidance:**
This is retry attempt **{retry_count} of {self.max_retries}**. Analyze the error and the arguments you provided. Do not repeat the exact same call. Consider the following before your next attempt:
1. **Invalid Parameters**: Does the error suggest that one or more arguments are incorrect, badly formatted, or missing? Review the tool's schema and your arguments.
2. **State or Preconditions**: Did a previous step fail or not produce the necessary state/resource for this tool to succeed?
3. **Alternative Approach**: Is this the right tool for the job? Could another tool or a different sequence of steps achieve the goal?
4. **Simplify the Task**: Can you break the problem down into smaller, simpler steps?
5. **Wrong Function Name**: Does the error indicates the tool is not found? Please check again and only use available tools.
Formulate a new plan based on your analysis and try a corrected or different approach.
"""
return ToolFailureResponse(
error_type=(
type(error).__name__
if isinstance(error, Exception)
else "ToolError"
),
error_details=str(error),
retry_count=retry_count,
reflection_guidance=reflection_message.strip(),
).model_dump(mode="json")
def _get_tool_retry_exceed_msg(
self,
tool: BaseTool,
tool_args: dict[str, Any],
error: Exception,
) -> dict[str, Any]:
"""Generates guidance when the maximum retry limit is exceeded."""
error_details = self._format_error_details(error)
args_summary = json.dumps(tool_args, indent=2, default=str)
reflection_message = f"""
The tool `{tool.name}` has failed consecutively {self.max_retries} times and the retry limit has been exceeded.
**Last Error:**
```
{error_details}
```
**Last Arguments Used:**
```json
{args_summary}
```
**Final Instruction:**
**Do not attempt to use the `{tool.name}` tool again for this task.** You must now try a different approach. Acknowledge the failure and devise a new strategy, potentially using other available tools or informing the user that the task cannot be completed.
"""
return ToolFailureResponse(
error_type=(
type(error).__name__
if isinstance(error, Exception)
else "ToolError"
),
error_details=str(error),
retry_count=self.max_retries,
reflection_guidance=reflection_message.strip(),
).model_dump(mode="json")