Skip to content

fix(core): 保留结构化输出混合批次中的业务工具历史 - #4086

Merged
juruoO-o merged 1 commit into
agentscope-ai:mainfrom
xihongshichaojidan8:codex/fix-4073-structured-output-history
Nov 11, 2026
Merged

juruoO-o merged 1 commit into
agentscope-ai:mainfrom
xihongshichaojidan8:codex/fix-4073-structured-output-history

Conversation

@xihongshichaojidan8

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT,验证基线为 main 的 5baff2613401bf87b3a967b185f2f14945ec594e。

Description

Fixes #4073.

问题与修复

降级结构化输出路径允许模型在同一响应中返回业务工具调用与内部 generate_response 调用。原来的 compressStructuredOutputContext 会删除整个混合消息,业务工具结果却仍保留,导致持久化历史以及下一轮请求出现没有对应 tool_use 的 tool_result。

本 PR 仅调整该清理方法:

  • 混合消息只移除内部输出工具内容块,保留业务调用及其配对结果,以及原消息的 ID、名称、角色、时间戳、非 usage metadata、text 和 thinking。
  • 纯输出工具消息和内部提醒继续按原规则删除;无关消息(包括空消息)原样保留。
  • 清理前已归集到最终结构化结果的 usage,不再保留到混合消息副本中,避免同一份使用量在历史里重复计数;最终返回值的 usage 不变,也不修改原消息对象。

不修改共用分类函数、usage/thinking 收集器、工具调度、权限、重试、native structured-output 路径或公共 API;不迁移此前已损坏的历史。

回归测试

新增 ReActAgentStructuredOutputCleanupTest,使用假模型和真实本地 Java 工具,不需要网络或模型凭证:

  • 输出工具在业务工具之前/之后、业务与输出分轮、纯输出四种情况,分别使用默认状态和显式 InMemoryAgentStateStore,共 8 个参数化场景。
  • 验证工具调用与结果的 ID 配对、内部输出工具内容被清理、业务工具不重复执行。
  • 读回持久化状态并重建 Agent,验证下一轮模型收到的历史仍完整。
  • 验证消息身份、非 usage 元数据、text/thinking、最终返回 usage,以及历史 usage 总量不重复;原消息对象保持不变。
  • 未修复实现上,4 个混合批次场景失败,4 个对照场景通过;修复后 8 个全部通过。

同步补充中英文 Agent 文档中的历史清理行为说明。

验证结果与边界

以下 Maven 检查在上述基线加本提交上完成,本地使用离线缓存和 central-only settings:

mvn -B -T1C -pl agentscope-core -am test
mvn -B -T1C -pl agentscope-core -am spotless:check
  • Core 全量:3031 项,0 failures、0 errors、9 skipped,BUILD SUCCESS。跳过项为现有的 7 个流契约占位测试、1 个需显式开启的压力测试、1 个仅 Windows 的测试。
  • 结构化输出相关针对性测试:37 项,0 failures、0 errors、1 skipped。
  • Spotless 与 git diff --check 通过。
  • Docs:npm test 13 项通过,npm run validate 通过,npm run broken-links 未发现坏链接。

尚未执行全仓库 clean verify、Windows 或真实模型厂商集成测试;不将这些列为已验证。

与相关 PR 的协调

提交前注意到 #4078 也针对 #4073 提供了实现,两个 PR 存在重叠。本实现同时包含非零 usage 的历史总量断言、原消息不变性和持久化后重建 Agent 的回归检查,保留混合消息时不再重复携带已归集到最终结果的 usage。这里说明本实现的设计与验证范围,并非对 #4078 未验证行为的缺陷判断。

欢迎维护者比较、选择或整合合适的实现与测试;不需要合并两个重复修复。

Checklist

  • Code has been formatted; Spotless checks pass.
  • All repository tests are passing (mvn test) — 已验证上述 core 范围,未执行全仓库测试。
  • Javadoc comments follow project conventions — 未新增公共 API。
  • Related documentation has been updated in English and Chinese.
  • Code is ready for review.

结构化输出清理仅移除内部输出工具内容,保留业务调用及其配对结果。沿用最终结果的 usage 归属,避免保留副本重复计数。

补充混合顺序、分轮、纯输出、状态存储与重建 Agent 的回归测试,并同步中英文说明。

Refs agentscope-ai#4073
@mintlify

mintlify Bot commented Nov 9, 2026

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 10ec8c0.

Approve preview

@juruoO-o juruoO-o left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修复必要,范围限定在结构化输出清理:保留混合消息中的业务工具配对,去除已归集的 usage,未改变公共 API 或工具调度。针对当前 head 与 main 的合并结果,先前本地验证的 ReActAgentStructuredOutputCleanupTest 8 项全部通过。本轮复核 head 未变;未发现阻塞问题。

@juruoO-o
juruoO-o merged commit c76e284 into agentscope-ai:main Nov 11, 2026
7 checks passed
chickenlj added a commit that referenced this pull request Nov 13, 2026
RiemannChow added a commit to RiemannChow/agentscope-java that referenced this pull request Nov 15, 2026
Upstream PR agentscope-ai#4086 already fixed agentscope-ai#4073 in ReActAgent#compressStructuredOutputContext:
mixed messages are rebuilt with the synthetic generate_response blocks removed and
CHAT_USAGE is dropped from the retained copy. Keep that implementation and remove
the now-unused stripStructuredOutputBlocks / isStructuredOutputBlock helpers from
this branch, so the two fixes do not coexist in the same method.

This branch keeps its javadoc clarification on the cleanup method and the
StructuredMixedBatchAuditTest regression suite, which still passes against the
upstream implementation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P0 重要紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: 结构化输出清理混合工具批次时丢失业务 tool_use,留下孤立 tool_result

3 participants