Skip to content

feat(agui): propagate userId from forwardedProps to RuntimeContext#2277

Closed
jujn wants to merge 2 commits into
mainfrom
feature/agui-runtime-context-user-id
Closed

feat(agui): propagate userId from forwardedProps to RuntimeContext#2277
jujn wants to merge 2 commits into
mainfrom
feature/agui-runtime-context-user-id

Conversation

@jujn

@jujn jujn commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

AgentScope-Java Version

[The version of AgentScope-Java you are working on, e.g. 1.0.12, check your pom.xml dependency version or run mvn dependency:tree | grep agentscope-parent:pom(only mac/linux)]

Description

[Please describe the background, purpose, changes made, and how to test this PR]

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

Copilot AI review requested due to automatic review settings July 18, 2026 12:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 62.50000% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...agentscope/core/agui/adapter/AguiAgentAdapter.java 62.50% 1 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

@AgentScopeJavaBot AgentScopeJavaBot added enhancement New feature or request area/ext/integration External protocols & middleware integrations labels Jul 19, 2026

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

🤖 AI Review

This is a clean, well-scoped change that propagates the userId field from AG-UI protocol's forwardedProps into RuntimeContext, enabling downstream agent code to identify the calling user. The implementation follows existing patterns in buildRuntimeContext, includes a defensive normalizeUserId helper that handles null/non-string/blank values, and ships with three focused unit tests covering the happy path, non-string type coercion, and missing-key scenarios. The code is correct and ready to merge with two minor nits below.

Comment on lines +183 to +184
if (userId == null) {
return null;

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.

[nit] Dead code: Object.toString() is guaranteed by the Java Language Specification to never return null, so the if (userId == null) branch is unreachable. You can safely remove lines 183-185 and simplify to:

private static String normalizeUserId(Object value) {
    if (value == null) {
        return null;
    }
    String userId = value.toString().trim();
    return userId.isEmpty() ? null : userId;
}

assertEquals("12345", context.getUserId());
}

@Test

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.

[nit] The normalizeUserId method has explicit trim + empty-check logic, but no test exercises the blank/whitespace-only userId edge cases (e.g. "" or " "). Consider adding a test like:

@Test
void testRunNormalizesBlankUserIdToNull() {
    // ... setup ...
    RunAgentInput input = RunAgentInput.builder()
        .threadId("t").runId("r")
        .messages(List.of(AguiMessage.userMessage("m", "hi")))
        .forwardedProps(Map.of(AguiAgentAdapter.FORWARDED_PROP_USER_ID_KEY, "  "))
        .build();
    adapter.run(input).collectList().block();
    assertNull(contextCaptor.getValue().getUserId());
}

This would give full branch coverage for the normalization helper.

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

🤖 AI Review

This is a clean, well-scoped change that propagates the userId field from AG-UI protocol's forwardedProps into RuntimeContext, enabling downstream agent code to identify the calling user. The implementation follows existing patterns in buildRuntimeContext, includes a defensive normalizeUserId helper that handles null/non-string/blank values, and ships with three focused unit tests covering the happy path, non-string type coercion, and missing-key scenarios. The code is correct and ready to merge with two minor nits below.

Comment on lines +183 to +184
if (userId == null) {
return null;

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.

[nit] Dead code: Object.toString() is guaranteed by the Java Language Specification to never return null, so the if (userId == null) branch is unreachable. You can safely remove lines 183-185 and simplify to:

private static String normalizeUserId(Object value) {
    if (value == null) {
        return null;
    }
    String userId = value.toString().trim();
    return userId.isEmpty() ? null : userId;
}

assertEquals("12345", context.getUserId());
}

@Test

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.

[nit] The normalizeUserId method has explicit trim + empty-check logic, but no test exercises the blank/whitespace-only userId edge cases (e.g. "" or " "). Consider adding a test like:

@Test
void testRunNormalizesBlankUserIdToNull() {
    // ... setup ...
    RunAgentInput input = RunAgentInput.builder()
        .threadId("t").runId("r")
        .messages(List.of(AguiMessage.userMessage("m", "hi")))
        .forwardedProps(Map.of(AguiAgentAdapter.FORWARDED_PROP_USER_ID_KEY, "  "))
        .build();
    adapter.run(input).collectList().block();
    assertNull(contextCaptor.getValue().getUserId());
}

This would give full branch coverage for the normalization helper.

@zouyx

zouyx commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

@copilot review

private RuntimeContext buildRuntimeContext(RunAgentInput input) {
return RuntimeContext.builder()
.sessionId(input.getThreadId())
.userId(normalizeUserId(input.getForwardedProp(FORWARDED_PROP_USER_ID_KEY)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

这样直接获取是否会存在身份伪造的风险?

@ningmeng0503 ningmeng0503 Jul 20, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

您说得很有道理,我完全认同RunAgentInput的参数确实需要校验,因为它们直接来自用户。不过我在想,是否可以把校验逻辑放在adapter之外的其他层来处理,这样职责会更清晰一些。

另外关于userId,我也觉得放在后台接口层从header或其他上下文中获取,再手动设置到RunAgentInput中,可能比让前端直接传递更合适,也更安全。

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@ningmeng0503
Can you take charge of this? You can wait until the event mechanism is upgraded to 2.0. I originally wanted to add a submission to your PR, but it can't be edited. You can continue in: #2044

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@jujn
I'd be happy to. I'll continue to follow up in #2044.

@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

This PR contains 115 lines of changes. Review in progress.


Automated review by github-manager-bot

@jujn jujn closed this Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/ext/integration External protocols & middleware integrations enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants