Skip to content

fix(agui): sanitize document rejection and map conversion errors - #3397

Merged
Buktal merged 3 commits into
agentscope-ai:mainfrom
xihongshichaojidan8:codex/fix-3392-agui-input-errors
Oct 9, 2026
Merged

Buktal merged 3 commits into
agentscope-ai:mainfrom
xihongshichaojidan8:codex/fix-3392-agui-input-errors

Conversation

@xihongshichaojidan8

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.0.4-SNAPSHOT,修复基线为官方 main 915507134。

Description

Refs #3392

本 PR 不增加 document 支持,仅修复现有拒绝路径中的内容回显和错误事件映射问题,提供一个限定范围的方案供维护者 review。

问题与修复

  • document 输入可以反序列化,但转换时的异常会拼接整个输入对象,包含 URL / Base64 和 metadata。改为明确、无 payload 的拒绝信息,保留现有 IllegalStateException 类型;InputContent 兜底异常仅报告类型名。
  • 消息和 resume 转换原先发生在已有同步错误捕获范围之外。将转换移入现有 try/catch,复用 errorEvents() / mapErrorCode(),不新建错误处理机制。
  • 默认转换错误序列为 RUN_STARTED → RUN_ERROR,document 拒绝的错误码为 INVALID_INPUT_ERROR;emitRunFinishedAfterError=true 继续保留错误后追加 RUN_FINISHED 的兼容行为。
  • 修正 DocumentInputContent 的误导性 Javadoc,并同步 EN/ZH AG-UI 说明。

范围与兼容性

仅涉及 AG-UI converter、adapter、相关测试及文档,共 7 个文件。不扩展 DataBlock、模型 provider、文档解析或沙箱上传;不修改 HTTP controller、异步流、取消与清理逻辑。

转换失败从原来的 raw error signal 改为协议错误事件,是本次有意的行为变化,也适用于同一转换入口的其他校验异常。运行上下文构建与 HTTP JSON 解析错误不在本次统一处理范围内。现有 RUN_STARTED.input 行为保持不变;本次保证的是拒绝错误信息不包含文档内容,并非对所有协议字段做脱敏。

因为本次只处理 Issue 中的干净拒绝路径,不自动关闭 #3392;真正的 document 功能支持可另行讨论。

验证

运行环境:OpenJDK 21.0.7(编译目标 Java 17),Node 22.22.2。

  • 新增 13 个回归用例:URL/Base64、默认/兼容终态序列、generic Agent / ReActAgent / 现有 HarnessAgent 测试替身,以及另一类结构化消息转换错误。检查错误信息不回显 source/metadata,且不调用 Agent 执行。修复前这些用例均捕获原有转换异常,修复后全部通过。
  • mvn -B -T1 -pl agentscope-extensions/agentscope-extensions-protocol/agentscope-extensions-agui -am verify:成功。Core 2518 个测试(跳过 9),AG-UI 520 个测试(无跳过),无失败或错误。
  • mvn -B -T1 clean verify:96 个模块构建成功;Surefire 报告汇总 9836 个测试,无失败或错误,跳过 212 个。跳过项主要涉及外部 E2E、Docker、provider 凭据和既有禁用测试,本次没有禁用或弱化任何测试。
  • 提交前再次运行 214 个 converter / adapter / request processor 相关测试:全部通过,无跳过;默认 Spotless 检查通过。
  • 文档 npm test(12 个测试通过)、npm run validate、npm run broken-links 均通过;git diff --check 通过。

未新增真实 HTTP document 上传集成测试或外部模型调用;未在 JDK 17 runtime 和 Windows 上本地验证。

Checklist

  • Code has been formatted with mvn spotless:apply
  • All enabled tests are passing(跳过项已在上方说明)
  • Javadoc comments follow project conventions
  • Related EN/ZH documentation has been updated
  • Code is ready for review

Map message conversion failures to the existing RUN_ERROR response while retaining the legacy finish-after-error option. Keep document input unsupported and omit its payload from rejection messages.

Refs agentscope-ai#3392
@codecov

codecov Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...cope/core/agui/converter/AguiMessageConverter.java 75.00% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@oss-maintainer oss-maintainer 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.

Summary

Scoped fix for #3392 and the design is right: conversion now happens inside the existing try/catch, so mapErrorCode() gives the caller RUN_STARTED -> RUN_ERROR(INVALID_INPUT_ERROR) instead of a raw error signal, the document rejection is payload-free, and the toString() echo ("Unhandled InputContent type: " + input) is gone. The generic fallback reporting only getClass().getSimpleName() is the correct rule, 13 regression cases across generic / ReActAgent / HarnessAgent with both URL and Base64 sources pin both terminal sequences, and the docs + Javadoc no longer promise a DataBlock mapping that never existed. Deliberately not approving: one payload-reflection path is still open and CI build jobs were still pending at review time.

Findings

  • [Warning] AguiAgentAdapter.java:161 — the sanitized RUN_ERROR is preceded by RUN_STARTED, and errorEvents() builds it with the caller's original RunAgentInput (AguiAgentAdapter.java:396), so the document URL / Base64 / metadata is still reflected over the stream and into anything that logs events. Pre-existing shared helper behaviour, not introduced here — but it is the remaining half of the leak this PR is about.
  • [Info] AguiMessageConverter.java:343 — the null arm is unreachable (List.copyOf in MessageContent.Blocks, sealed InputContent fully handled); the sibling toSource() fallback at line 365 still stringifies the Base64 value and deserves the same treatment.

CI / CLA

CLA signed (license/cla=success). validate, Check License, Check Module Sync green; build (ubuntu-latest) and build (windows-latest) were pending on 3b481ef8. Resolving the RUN_STARTED.input point (guard it, or state in docs/v2/*/agui.md that only RUN_ERROR is sanitized) plus a green build is enough for an approval — happy to re-review on the next push.


Automated review by github-manager-bot

// Convert AG-UI messages and official resume entries to AgentScope
// messages.
List<Msg> msgs =
messageConverter.toMsgList(

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.

Routing conversion into errorEvents(threadId, runId, input, error, true) (line 167) does sanitize RUN_ERROR, but it also newly emits a RUN_STARTED built as new AguiEvent.RunStarted(threadId, runId, null, input) (AguiAgentAdapter.java:396), and RunStarted.input is a serialized field — so the rejected DocumentInputContent (URL with query token, Base64 body, metadata) is reflected back to the caller on a path where, before this PR, conversion failures produced no events at all. AguiInputErrorTest asserts only error.message(), so the test suite cannot see the echo. Passing includeRunStarted=true is fine for the protocol sequence; what undercuts the stated goal is the payload riding along in it. Consider new AguiEvent.RunStarted(threadId, runId) on the synchronous-error path (or a copy of input with message content stripped), and extend the assertion to the whole collected event list rather than just events.get(1).

}
throw new IllegalStateException(
"Unhandled InputContent type: "
+ (input == null ? "null" : input.getClass().getSimpleName()));

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.

The input == null ? "null" arm is unreachable: the only caller iterates MessageContent.Blocks.parts(), whose compact constructor runs List.copyOf (MessageContent.java:69) and rejects null elements, and InputContent is sealed over exactly the five subtypes handled above (InputContent.java:50-55), so a future subtype is still a non-null instance. The class-name-only rule is the right one, but the sibling fallback at line 365 still breaks it: "Unhandled InputContentSource type: " + inputSource stringifies an InputContentDataSource including its Base64 value. Mirroring line 343 there closes the last payload echo in this file.

@xihongshichaojidan8

Copy link
Copy Markdown
Contributor Author

感谢 review。按建议选择明确文档边界、保留现有事件语义的方案:已在中英文 AG-UI 文档说明,保护仅针对 RUN_ERROR 的错误消息;此前的 RUN_STARTED.input 仍保留原始请求(包括被拒绝的文档内容),记录或转发事件的使用方需要自行脱敏。确实,本次将转换异常映射成事件后,这条原先没有事件的失败路径现在也会携带输入;本 PR 不承诺整个事件流脱敏,也未修改共享的事件 payload 行为。

关于 toSource():当前 InputContentSource 是仅允许 URL/Data 两种类型的 sealed 接口,这两种类型都会在兜底前返回,因此现有 Base64 输入不会走到字符串拼接分支。该兜底对未来类型扩展的风险可以另行评估,本次不扩大修改范围。新增 null 测试仅验证私有方法的防御行为,不表示正常 Blocks 输入允许 null,也未伪造未知类型来补覆盖率。

本地 63 个相关 Java 测试全部通过、无跳过;本次文档的 npm test、严格构建验证与链接检查均通过。线上构建和覆盖率请以最新提交的检查结果为准。欢迎再 review。

@oss-maintainer oss-maintainer 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.

Summary

Re-review of the two commits pushed since my last pass (6a5627c6, 4b2233cf). Both open findings from the previous round are closed, and the delta itself is sound — a test that pins the new guard and a docs paragraph that states the boundary. Approving; CI detail and one carry-over note below.

On the RUN_STARTED.input reflection (AguiAgentAdapter.java:161) — I accept your reasoning and your resolution. RUN_STARTED carrying input is mandated by the AG-UI state-snapshot semantics, not incidental to this PR, and turning it into a payload-free event would be a protocol-visible change that belongs in its own discussion. Documenting the boundary in both docs/v2/en and docs/v2/zh — stating that only the RUN_ERROR message is sanitized and that consumers who log or forward events must redact — is the right outcome for a scoped fix, and the matching RunStarted(threadId, runId) overload means a future hardening step has a cheap path if the project ever decides to take it. Closed, no action needed.

On the unreachable null arm (AguiMessageConverter.java:343) — keeping it as a defensive guard and proving it with testNullInputContentIsRejectedWithoutDereferencingIt is a better outcome than deleting it. Closed.

Findings

  • [Info] AguiMessageConverter.java:365 — carried over, unchanged by these commits: the sibling toSource() fallback still does "Unhandled InputContentSource type: " + inputSource, which stringifies the whole source, so an InputContentDataSource would put its Base64 value into the exception message that this PR is working to keep clean. InputContentSource is sealed over exactly InputContentUrlSource / InputContentDataSource and both arms are handled, so the fallback is unreachable today — not a blocker, but the next permitted subtype makes it live. inputSource == null ? "null" : inputSource.getClass().getSimpleName() would make the two guards consistent, in either this PR or a follow-up.

CI at review time

validate, Check License, Check Module Sync, build (ubuntu-latest) passed and license/cla is signed on 4b2233cf; build (windows-latest) was still queued when this review was submitted. The added test only relies on getDeclaredMethod + setAccessible on a classpath type, so it is platform-independent and I do not expect the Windows job to diverge — flagging it so the approval is read as "code approved, one build job still running" rather than "all green".

Thanks for the fast turnaround and for keeping the doc wording precise in both languages.


Automated review by github-manager-bot

}
throw new IllegalStateException(
"Unhandled InputContent type: "
+ (input == null ? "null" : input.getClass().getSimpleName()));

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.

[Info] Closing the loop on the previous round: keeping the null arm as a defensive guard and pinning it with testNullInputContentIsRejectedWithoutDereferencingIt is a better outcome than deleting it, and the test's own comment ("Blocks rejects null elements, so exercise this defensive guard directly") already explains why it reaches in by reflection. No action here.

The one item still open is the sibling fallback at line 365: "Unhandled InputContentSource type: " + inputSource stringifies the whole source, so an InputContentDataSource would land its Base64 value in the message — the same shape this PR removes above. InputContentSource is sealed over exactly InputContentUrlSource / InputContentDataSource, both handled, so it is unreachable today and not a blocker; the next permitted subtype would make it live. inputSource == null ? "null" : inputSource.getClass().getSimpleName() would make the two guards consistent, here or in a follow-up.

@Buktal Buktal 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.

LGTM

@Buktal
Buktal merged commit a732b63 into agentscope-ai:main Oct 9, 2026
7 checks passed
@jujn jujn added the 2.0.5 label Oct 10, 2026
chickenlj pushed a commit that referenced this pull request Nov 13, 2026
Backport the verified fix to the 2.0.x implementation and retain its existing public contracts where the upstream kernel differs.

(cherry picked from commit a732b63)
Backport-review-patch: 857d588e0299555e25ba165918367560f54ea3b6
Constraint: No AgentScope Service or SDK migration
Confidence: high
Not-tested: Whole-repository verification pending after lane integration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants