Skip to content

fix(agentanalytics): suppress duplicate TOOL_COMPLETED and handle ReflectAndRetryToolPlugin errors - #1776

Open
haiyuan-eng-google wants to merge 1 commit into
google:mainfrom
haiyuan-eng-google:fix/bqaa-1745-recovered-tool-error
Open

haiyuan-eng-google wants to merge 1 commit into
google:mainfrom
haiyuan-eng-google:fix/bqaa-1745-recovered-tool-error

Conversation

@haiyuan-eng-google

Copy link
Copy Markdown

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

2. Description of the change:

Problem:
When a tool failure is intercepted and recovered into a replacement result Map by OnToolErrorCallback, a downstream plugin, or ReflectAndRetryToolPlugin, BigQueryAgentAnalyticsPlugin previously either:

  1. Emitted both TOOL_ERROR (in onToolErrorCallback) and a duplicate TOOL_COMPLETED (status = "OK", in afterToolCallback) for the same tool span when BigQueryAgentAnalyticsPlugin ran before the recovering callback, or
  2. Never ran onToolErrorCallback at all when ReflectAndRetryToolPlugin was registered ahead of BigQueryAgentAnalyticsPlugin (short-circuiting PluginManager.onToolErrorCallback) and logged the ERROR_HANDLED_BY_REFLECT_AND_RETRY_PLUGIN guidance map as TOOL_COMPLETED (status = "OK") instead of TOOL_ERROR (status = "ERROR").

Solution:

  • Track recorded tool errors per ToolCallKey(invocationId, branch, operationId) in a bounded toolErrorRecorded cache (maximumSize(10_000), expireAfterWrite(Duration.ofMinutes(10))), invalidating stale entries in beforeToolCallback, marking immediately before logEvent("TOOL_ERROR", ...) in logToolErrorEvent, and atomically consuming via toolErrorRecorded.asMap().remove(...) in afterToolCallback to suppress duplicate TOOL_COMPLETED rows while isolating sibling ParallelAgent branches, concurrent empty-functionCallId calls (syntheticToolCallIds), and wrapper ToolContext allocations.
  • Detect "ERROR_HANDLED_BY_REFLECT_AND_RETRY_PLUGIN".equals(result.get("response_type")) in afterToolCallback after popping the "tool" span and emit TOOL_ERROR (status = "ERROR", extracting error_details -> error_type -> default fallback with maxContentLength truncation and is_truncated propagation) regardless of plugin registration order.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Ran the full agentanalytics test suite (235 tests across 10 test classes, 0 failures) and fmt:check:

  • ./mvnw -B -pl core -am -Dtest='com.google.adk.plugins.agentanalytics.*Test' test (Tests run: 235, Failures: 0, Errors: 0, Skipped: 0, including afterToolCallback_afterRecoveredToolError_doesNotEmitDuplicateToolCompleted, afterToolCallback_concurrentIdLessTools_recoveredErrorDoesNotSuppressSibling, afterToolCallback_parallelBranchesAndWrapperContexts_isolatesErrorMarker, and afterToolCallback_reflectAndRetryPluginFirst_emitsToolErrorWithoutToolCompleted).
  • ./mvnw -B fmt:check -pl core (Processed 485 files (0 non-complying)).

Checklist

  • I have read the CONTRIBUTING.md document.
  • My pull request contains a single commit.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • Any dependent changes have been merged and published in downstream modules.

…lectAndRetryToolPlugin errors

- Track recorded tool errors per (invocation, branch, toolOperationId)
  in onToolErrorCallback (cleared in beforeToolCallback and marked right
  before logEvent) so afterToolCallback skips emitting a duplicate
  TOOL_COMPLETED row when another plugin or callback recovers a failure.
- Isolate id-less concurrent tool calls via weak-key identity tracking on
  ToolContext while preserving non-empty functionCallId matching across
  fresh wrapper contexts.
- When ReflectAndRetryToolPlugin runs ahead of BigQueryAgentAnalyticsPlugin
  and short-circuits onToolErrorCallback (or converts a result error),
  detect ERROR_HANDLED_BY_REFLECT_AND_RETRY_PLUGIN in afterToolCallback
  and emit TOOL_ERROR (status=ERROR) instead of TOOL_COMPLETED.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant