Skip to content

[Bug]:ToolResultEvictionMiddleware does not evict #1695

Open

Description

@thetimeofsummer

Bug: ToolResultEvictionMiddleware does not evict — doOnComplete fires before tool results are written to context

Problem

ToolResultEvictionMiddleware never evicts oversized tool results. The evictAddedToolResults() callback always finds zero new messages and exits immediately.

Root Cause

The onActing middleware hook wraps only actingStream(), which emits streaming events (ToolResultEndEvent, etc.) but does not write results to AgentState.contextMutable().

Tool result messages are written to context in notifyPostActingHook() — which is called in acting()'s outer .flatMap(), after the middleware chain has already completed.

Execution order:

actingStream() Flux completes
  → doOnComplete(() -> evictAddedToolResults()) fires   ← eviction runs HERE
  → contextMutable()[sizeBefore..end] is EMPTY           ← nothing to evict
  → (outer flatMap) notifyPostActingHook()               ← results written HERE (too late)
      → state.contextMutable().add(resultMsg)

Relevant lines in ReActAgent.java:

  • 2129–2138: middleware chain wraps only actingCore = actingStream()
  • 2173–2174: notifyPostActingHook() called outside the middleware chain
  • 2728–2729: state.contextMutable().add(resultMsg) — actual write point

Fix Suggestion

Bring notifyPostActingHook() inside the middleware-wrapped scope so that tool results are in contextMutable() before the onActing Flux completes.

Option A (minimal): Extend actingCore to include the post-acting context write, then remove the duplicate call from the outer flatMap:

Function<ActingInput, Flux<AgentEvent>> actingCore = ai ->
    actingStream(ai.toolCalls(), replyId, resultHolder)
        .thenMany(
            Mono.defer(() -> Flux.fromIterable(resultHolder.get())
                    .concatMap(this::notifyPostActingHook)
                    .then(Mono.empty())));

Option B (alternative): Move eviction to the onReasoning hook's pre-step — check contextMutable() for oversized TOOL messages at the start of each reasoning round, before building the prompt. This avoids restructuring actingCore entirely.

Activity

  1. added
    bug
    on Jun 10, 2026
  2. added
    area/core/agent
    on Jun 10, 2026
  3. wahllllll commented on Oct 3, 2026

    @wahllllll
    Contributor

    This no longer matches the current implementation, so it looks closable.

    ToolResultEvictionMiddleware stopped hooking the acting stream back in #1703 (ea363f17, 2026-06-11 — the day after this issue was filed). It now participates at ExtensionPoint.ON_REASONING, which runs before the next model call rather than at the end of the acting stream, and onReasoning evicts in two passes:

    1. AgentState.contextMutable() first — so tool results already written to canonical state are caught even if a pre-reasoning hook removed or rewrote them in the model view;
    2. the current ReasoningInput view — reusing the canonical replacements so the same result is not written twice.

    The class javadoc states exactly this ordering as the reason, and ToolResultEvictionMiddlewareTest drives onReasoning directly in eight places, covering the oversized-result path plus the already-evicted, excluded-tool and failed-write branches.

    The doOnComplete-fires-before-results-are-written mechanism described here is gone. If you are still seeing results that never get evicted on current main, that would be a new report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/core/agentbug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions