Skip to content

fix: parallel tool metrics - #4193

Merged
VascoSch92 merged 2 commits into
OpenHands:mainfrom
luciobaiocchi:fix/4189-parallel-tool-metrics
Jul 23, 2026
Merged

VascoSch92 merged 2 commits into
OpenHands:mainfrom
luciobaiocchi:fix/4189-parallel-tool-metrics

Conversation

@luciobaiocchi

Copy link
Copy Markdown
Contributor

HUMAN:

This is a fix follow-up to #4146, raised by @VascoSch92 in the approving review.


AGENT:

Why

The per-request metrics subtitle matches an event to its usage by llm_response_id.
When one LLM response emits multiple parallel tool calls, every resulting
ActionEvent shares that one llm_response_id and one TokenUsage, so the
identical per-request line was rendered under each sibling:

Action 1 : ↑ input 2K (total 3K) • ...
Action 2 : ↑ input 2K (total 3K) • ...   ← same request, repeated
Action 3 : ↑ input 2K (total 3K) • ...   ← same request, repeated

The numbers are correct but read as three separate 2K requests. Totals are
unaffected. Fixes #4189.

Summary

  • Only the first ActionEvent of a batch (same llm_response_id) shows the
    per-request numbers; siblings fall back to the labeled totals-only format from
    fix(visualizer): show per-request token usage alongside cumulative to… #4146 (↑ input (total 3K) • …).
  • _claim_batch_primary(event) decides this in O(1) by tracking only the
    previous ActionEvent's llm_response_id. A batch's events always render
    consecutively (one LLM response emits all its tool calls before the next
    response exists), so a single remembered id is enough to tell a batch primary
    from its siblings — no scan of the event stream. It's named to signal that it
    mutates, and must be called once per rendered event.
  • _format_metrics_subtitle gains a keyword-only force_totals flag; the two
    render entry points (DefaultConversationVisualizer._create_event_block and
    DelegationVisualizer's action path) pass force_totals=not _claim_batch_primary(event).
  • Single tool calls and non-ActionEvents are always their own primary, so
    their output is unchanged.

Design decision: O(1) tracking vs. stateless scan

The first draft determined the batch primary statelessly, by scanning
state.events for the first ActionEvent sharing the response id. That was
idempotent and replay-safe, but it is O(N) per rendered ActionEvent — the
primary renders before it is appended to state, so the scan walks the whole
conversation to the end — i.e. O(M²) over an M-event trajectory, against a
disk-backed (cached) event store.

The human author (Lucio) preferred keeping the visualizer cheap on long
benchmark / eval trajectories and asked for the O(1) approach even though it
carries a little render-time state. We adopted the O(1) previous-response_id
tracking above. It is not fully idempotent (rendering the exact same event twice
in isolation would flip primary→sibling), but that is a test-only artifact: in
production each event streams through on_event exactly once and batches are
consecutive, and because it remembers only the immediately-preceding id, a full
replay still re-derives the correct result — so it stays replay-safe, strictly
better than a growing set of shown ids.

Two mitigations keep the small residual risk in check: the method name signals
its mutation, and test_batch_action_events_are_emitted_consecutively
(tests/sdk/agent/test_response_dispatch.py) pins the consecutive-emission
invariant the O(1) shortcut relies on — if a future change interleaves event
emission, that test fails before the metrics silently mislabel.

Considered alternative (possible follow-up): mark the batch index on the event

The cleanest design would make the decision a pure function of the event —
record the tool-call's batch index on ActionEvent at construction (the agent
already computes it: for i, tool_call in enumerate(message.tool_calls) in
response_dispatch.py, branching on i == 0), so the visualizer would just read
event.batch_index == 0. That removes the mutation and the consecutive-emission
assumption while staying O(1), and a real batch index could also simplify the
batch regrouping in events_to_messages.

It is intentionally not done here: Event is extra="forbid", so adding a
persisted field to a core event type carries the same backward-compat risk that
closed #4147 (older readers rejecting new events), and it touches the sync/async
construction paths, the ACP agent, and many tests. That is a data-model change
that deserves its own PR and maintainer sign-off on the event-schema/compat
policy, rather than riding along in a visualizer fix.

Issue Number

Fixes #4189

How to Test

uv run pytest tests/sdk/conversation/test_visualizer.py tests/tools/delegate/test_visualizer.py tests/sdk/agent/test_response_dispatch.py -v

Four new tests: _claim_batch_primary tracked across consecutive batches;
end-to-end _create_event_block assertions on the default and delegate
visualizers (primary shows per-request, siblings show totals-only); and
test_batch_action_events_are_emitted_consecutively, which pins the emission
invariant the O(1) detection relies on. All 43 visualizer tests and the full
tests/sdk/conversation + tests/tools/delegate + tests/sdk/agent/test_response_dispatch
suites pass; ruff and pyright clean.

Video/Screenshots

Simulating the real callback order (each event renders before it is appended to
state), a 3-way parallel batch after a prior request (running total 3K/300):

c1 (primary): Tokens: ↑ input 2K (total 3K) • cache hit 0.00% (total 0.00%) • ↓ output 200 (total 300) • $ (total 0.00)
c2 (sibling): Tokens: ↑ input (total 3K) • cache hit (total 0.00%) • ↓ output (total 300) • $ (total 0.00)
c3 (sibling): Tokens: ↑ input (total 3K) • cache hit (total 0.00%) • ↓ output (total 300) • $ (total 0.00)

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

@luciobaiocchi
luciobaiocchi force-pushed the fix/4189-parallel-tool-metrics branch 2 times, most recently from ebb3b39 to ade86c1 Compare July 22, 2026 15:40
@luciobaiocchi
luciobaiocchi marked this pull request as ready for review July 22, 2026 15:51
@VascoSch92 VascoSch92 changed the title Fix/4189 parallel tool metrics fix: parallel tool metrics Jul 23, 2026
…a parallel batch

Parallel tool calls all come from one LLM response, so they share a single
llm_response_id and a single per-request TokenUsage. The per-request metrics
subtitle from OpenHands#4105 was therefore rendered identically under every sibling of
the batch, reading as N separate requests worth of tokens (OpenHands#4189).

Only the first ActionEvent of a batch (the batch primary) now shows the
per-request numbers; siblings fall back to the labeled totals-only format.
A batch's events are always rendered consecutively (one LLM response emits all
its tool calls before the next response exists), so the primary is detected by
tracking just the previous ActionEvent's response id -- O(1) per event, no scan
of the event stream, which keeps rendering cheap on long benchmark trajectories.
The detector (_claim_batch_primary) is named to signal that it mutates, and a
test in test_response_dispatch.py pins the consecutive-emission invariant it
relies on. Single tool calls and non-ActionEvents are always their own primary,
so their output is unchanged.
@luciobaiocchi
luciobaiocchi force-pushed the fix/4189-parallel-tool-metrics branch from ade86c1 to 0040c84 Compare July 23, 2026 09:00
@VascoSch92
VascoSch92 merged commit 6864c13 into OpenHands:main Jul 23, 2026
28 checks passed
@VascoSch92

Copy link
Copy Markdown
Member

@luciobaiocchi Thanks :-D

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.

Visualizer: parallel tool calls repeat the same per-request metrics under every ActionEvent of the batch

2 participants