Skip to content

Commit 5a33974

Browse files
committed
fix: deduplicate Arraylake snippets and skip failed tool calls
Snippets for retrieve_era5_data are now: - Filtered by checking ToolMessage for error keywords (limit, failed, etc.) - Deduplicated by (variable_id, region) key — one snippet per unique combo - This prevents 4+ Arraylake buttons when agent retries or year-splits
1 parent c9f3b9b commit 5a33974

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

web/agent_wrapper.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,43 @@ async def process_message(
216216
await asyncio.sleep(0.5)
217217

218218
# Collect Arraylake snippet from NEW messages only
219+
# Only emit ONE snippet per unique (variable, region) — skip failed calls
219220
arraylake_snippets = []
220-
for msg in new_messages:
221+
seen_snippet_keys = set()
222+
for i, msg in enumerate(new_messages):
221223
if hasattr(msg, 'tool_calls') and msg.tool_calls:
222224
for tc in msg.tool_calls:
223225
if tc.get('name') == 'retrieve_era5_data':
226+
# Check if tool call succeeded by looking at the next message
227+
# (ToolMessage with same tool_call_id)
228+
tc_id = tc.get('id', '')
229+
succeeded = True
230+
for later_msg in new_messages[i+1:]:
231+
if (hasattr(later_msg, 'tool_call_id') and
232+
later_msg.tool_call_id == tc_id):
233+
content = getattr(later_msg, 'content', '') or ''
234+
if any(kw in content.lower() for kw in
235+
['error', 'failed', 'exception', 'limit',
236+
'exceeded', 'rejected', 'too large']):
237+
succeeded = False
238+
break
239+
240+
if not succeeded:
241+
continue
242+
224243
args = tc.get('args', {})
244+
# Dedup key: variable + rounded region
245+
dedup_key = (
246+
args.get('variable_id', 'sst'),
247+
round(args.get('min_latitude', -90)),
248+
round(args.get('max_latitude', 90)),
249+
round(args.get('min_longitude', 0)),
250+
round(args.get('max_longitude', 360)),
251+
)
252+
if dedup_key in seen_snippet_keys:
253+
continue
254+
seen_snippet_keys.add(dedup_key)
255+
225256
arraylake_snippets.append(_arraylake_snippet(
226257
variable=args.get('variable_id', 'sst'),
227258
query_type=args.get('query_type', 'spatial'),

0 commit comments

Comments
 (0)