Skip to content

fix(core): tolerate stale tool results on resume instead of wedging the session - #3766

Open
aiyili wants to merge 1 commit into
agentscope-ai:mainfrom
aiyili:fix/stale-tool-results
Open

aiyili wants to merge 1 commit into
agentscope-ai:mainfrom
aiyili:fix/stale-tool-results

Conversation

@aiyili

@aiyili aiyili commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3043

Problem

Transcript-replaying clients (AG-UI, CopilotKit-style) resend results that were already consumed in an earlier round, together with the new message. validateAndAddToolResults threw on any result id outside the pending set, and it threw before anything reached the context: the pending round survived unchanged, the stale result stayed in the client's transcript, and every later message in the thread failed the same way.

Fix

Stale results are removed from the input itself, before any "the caller provided results" decision is taken. Everything downstream then behaves exactly as if they had never been sent: nothing stale is persisted or published, and the remaining checks (duplicate ids, partial results with text) are unchanged.

A stale result says nothing about the calls that are still pending, so ignoring it is kept apart from cancelling them:

input after removing stale results behaviour
contains the current result(s) normal resume
nothing left — a pure replay nothing is resumed, patched or persisted; the agent answers TOOL_SUSPENDED, the pending calls stay as they are
user text only treated as "no results supplied": the calls stay pending and are cancelled only by enablePendingToolRecovery (opt-in), with the user's text kept; without it the submission is rejected with an error naming the pending calls

A pure replay deliberately does not take the existing "no input → resume" path: that path dispatches the pending calls again.

Tests

ReActAgentStaleToolResultTest, 15 cases: mixed valid/stale blocks in separate and in the same message; all-stale results with user text, recovery off and on; a later legitimate result for a still-running call; no re-dispatch on replay; request/result bookkeeping under the RequireExternalExecutionEvent reply id (mixed submission, pure replay, rejected submission, recovery cancellation); the unchanged duplicate-id and partial-results-with-text checks.

With the removal of stale results disabled, 11 of the 15 fail; the other four guard behaviour that did not change.

@aiyili

aiyili commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Spotless violation on my pushed file (the server-side-constructed content skipped the local format pass — the standard step I keep re-learning). Fixed in 3d4c5a3 (formatting only, verified the fix content is intact). Rerun should be green.

@juruoO-o

Copy link
Copy Markdown
Collaborator

Two correctness issues remain in validateAndAddToolResults():

  1. [P1] Stale blocks are filtered for validation/events but still persisted. The patch filters the local results list and providedIds, but the normal path still appends the original msgsToPersist using state.contextMutable().addAll(msgsToPersist). With one current result plus one stale result, the stale ToolResultBlock remains in model history despite the "Dropping stale" log. Please filter the message content actually persisted as well, preserving legitimate non-tool content and returnDirect placeholder behavior.

  2. [P2] An all-stale replay cancels unrelated pending work and drops the accompanying input. The early-return branch synthesizes error results for every current pending call, even though receiving old results does not establish that those new calls were cancelled. It returns before appending the caller's user text and before the normal ExternalExecutionResultEvent/reply-id cleanup. In the advertised "stale result + new user text" scenario, that new instruction is therefore not added here. Please separate ignoring stale results from an explicit cancellation policy, preserve the user input, and keep interaction bookkeeping consistent with the chosen behavior.

Please add regressions for mixed valid/stale blocks, all-stale results accompanied by user text, a later legitimate result for a still-running call, and pending interaction/reply-id handling.

Reviewed head: 175995fd456950b77eb0111c8c26676b96858b9b. Static source review; local Java/Maven tests were not run.

转录重放型客户端(AG-UI / CopilotKit 式)会连同新消息重发早先轮次已
消费的 tool result。validateAndAddToolResults 对 pending 集合之外的
任何 id 都抛 IllegalStateException,而异常发生在消息入上下文之前——
pending 轮原样留存,陈旧结果也留在客户端转录里,此后该会话每条消息
都以同样方式失败。

修复:在进入任何"调用方提供了结果"的判定之前,把不属于 pending 调用
的结果块从输入中剔除(按块剔除,保留当前结果、用户文本与其他内容)。

评审意见(agentscope-ai#3766)对应:
- P1 陈旧块被过滤却仍持久化:剔除作用于输入本身,持久化的
  msgsToPersist 与发布的事件都不再包含陈旧结果。
- P2 全陈旧重放被当成取消:不再有"全陈旧即合成错误结果"的分支。
  - 输入只含陈旧结果(纯重放):调用方对 pending 调用没有任何新指示,
    因此不恢复执行、不修补、不写入,直接返回 TOOL_SUSPENDED——pending
    调用保持原样。(不能走既有的"无输入则 resume"路径:那会重新分发
    pending 调用。)
  - 陈旧结果附带用户文本:剔除后等同"未提供结果"——pending 调用保持
    pending;只有显式开启 enablePendingToolRecovery 时才由既有恢复
    逻辑取消它们,且用户文本照常进入上下文。

其余校验不变:重复 id、部分结果附带文本仍然报错。

请求记账:挂起的外部调用是一个 pending 请求,以 RequireExternalExecution
的 replyId 标识。剔除陈旧结果不影响它——请求仍由真实结果、在同一
replyId 下恰好结算一次;纯重放不发布 ExternalExecutionResultEvent,
pending 请求及其 replyId 原样保留。

测试:ReActAgentStaleToolResultTest 共 15 例——混合有效/陈旧(跨消息
与同消息)、陈旧结果不进入模型输入、纯重放不报错且不重新分发 pending
调用、陈旧结果附带用户文本(未开/已开 recovery)、仍在运行的调用事后
收到真实结果、结果事件与 replyId 记账(混合提交/纯重放/被拒绝的提交/
recovery 取消)、重复 id 与部分结果附带文本仍被拒绝。
@aiyili
aiyili force-pushed the fix/stale-tool-results branch from 175995f to 1a88c47 Compare November 26, 2026 14:16
@aiyili

aiyili commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@juruoO-o Both issues are addressed in 1a88c478. The change is re-implemented on current main as a single commit; the old head is kept on backup/pr-3766-before-rework.

P1 — stale blocks were filtered but still persisted

Stale results are now removed from the input itself, before any "the caller provided results" decision is taken. What gets persisted and what gets published on ExternalExecutionResultEvent therefore never contain a stale block. Non-tool content and the returnDirect placeholder path are untouched, because they now see the same message list minus the stale blocks.

P2 — an all-stale replay cancelled pending work and dropped the input

The "all stale → synthesize error results for every pending call" branch is gone. Ignoring a replay and cancelling calls are now separate things:

input after removing stale results behaviour
contains the current result(s) normal resume
nothing left — a pure replay nothing is resumed, patched or persisted; the agent answers TOOL_SUSPENDED and the pending calls stay exactly as they are
user text only treated as "no results supplied": the calls stay pending. They are cancelled only by enablePendingToolRecovery, which is opt-in, and the user's text is kept. Without it the submission is rejected with an error that names the pending calls

One detail worth flagging on the pure-replay row: I first routed it through the existing "no input → resume" path, on the reasoning that it is equivalent to the caller having sent nothing. It is not — that path dispatches the pending calls again, so every replay would re-run the tool. A regression with a dispatch counter caught it (expected: <1> but was: <4> after three replays), and the row above is the corrected behaviour.

Regressions you asked for

ReActAgentStaleToolResultTest, 15 cases:

  • mixed valid/stale blocks, in separate messages and in the same message;
  • all-stale results accompanied by user text, with recovery off (rejected, pending calls untouched) and on (cancelled, text reaches the model);
  • a later legitimate result for a still-running call, after an all-stale submission and again beside another replayed one;
  • pending interaction / reply-id bookkeeping: the result event is published exactly once, under the RequireExternalExecutionEvent's reply id, with the current result only; a pure replay publishes no result event and leaves the request pending under the same id; a rejected submission leaves it untouched; a recovery cancellation is not reported as an external result;
  • unchanged checks: duplicate ids and partial-results-with-text are still rejected.

With the removal of stale results disabled, 11 of the 15 fail; the other four guard behaviour that did not change.

agentscope-core and agentscope-harness suites pass on current main (same two symlink cases as on unmodified main on a Windows host without that privilege).

One policy point is yours to decide: I kept "stale results + user text" as an error unless enablePendingToolRecovery is on, because cancelling calls that may still be running should be an explicit choice. If you would rather have that case cancel by default, it is a small change.

This branch has not been deployed

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

Labels

P1 紧急不重要

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:: Message: Invalid tool result IDs: [call_01_w7wlz4ch9Hiw4Um74zwv8117]. Expected: [call_00_OugZmGqpB71ykFO5IjxM8342]

3 participants