Skip to content

feat(core): allow setting a custom agentId via builder, falling back to a random UUID when absent - #3360

Merged
chickenlj merged 5 commits into
agentscope-ai:mainfrom
xzxiaoshan:feat/agent-id
Oct 8, 2026
Merged

chickenlj merged 5 commits into
agentscope-ai:mainfrom
xzxiaoshan:feat/agent-id

Conversation

@xzxiaoshan

@xzxiaoshan xzxiaoshan commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

What

Adds an opt-in agentId to ReActAgent.builder():

ReActAgent.builder().name("assistant").agentId("my-stable-agent-id")...

When not set (null or blank), the existing random-UUID behavior is preserved — no
change for users who don't opt in. One exception for existing HarnessAgent users:
HarnessAgent.Builder.agentId(...) is now forwarded to the inner agent, so
getAgentId() returns the configured id instead of the inner agent's generated UUID.

Changes

  • AgentBase: new constructor overload (name, description, hooks, agentId); null/blank
    falls back to UUID.randomUUID(). Existing constructors delegate with null.
  • ReActAgent.Builder: new agentId field + setter.
  • ReActAgent: passes builder.agentId to AgentBase.
  • Test: explicit id is used as-is; blank falls back to a generated UUID.

Relationship to #2395

Same goal as #2395, but as a minimal change: reuses the existing agentId /
getAgentId() instead of introducing an additional id property. Feel free to merge
either one and close the other.

@xzxiaoshan xzxiaoshan changed the title feat(core): support setting a custom agentId, falling back to a rando… feat(core): allow setting a custom agentId via builder, falling back to a random UUID when absent Sep 29, 2026
@codecov

codecov Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

oss-maintainer

This comment was marked as outdated.

oss-maintainer

This comment was marked as outdated.

oss-maintainer

This comment was marked as outdated.

@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 after e6815e27 (new commits since my last review at 1b8f7987). The single-file delta closes every point left open last round, and it does it in the way I'd asked: all three message-bus call sites in build() (teamsMw.wireMessageBus, wireTaskRepositoryMessageBus(...), subagentsMw.wireMessageBus) now take resolvedAgentId instead of the raw agentId field, so the wake-up key, the state dir and the filesystem namespace key are derived from exactly one value; the harness setter no longer pre-normalises (normalisation happens once, in build()), which also removes the core/harness validation-timing asymmetry; and the name fallback is now explicitly documented as unvalidated. The comment above AgentBase.normalizeAgentId(agentId) in build() makes the ordering intent ("validate before any component consumes the id") durable.

Verdict

Approved. The change stays additive — unset/blank agentId keeps the generated UUID and the name-based namespace fallback, so existing users are unaffected — validation is centralised in AgentBase.normalizeAgentId, the id is checked before any Files.createDirectories on a derived path, and both the core (ReActAgentTest) and harness (HarnessAgentIdTest) layers have coverage, including the traversal rejection. CLA is signed and CI at e6815e27 is green (build (ubuntu-latest), build (windows-latest), Check License, Check Module Sync, codecov/patch).

Findings

Two non-blocking follow-ups, both as inline comments:

  • [Info] agentscope-harness/.../HarnessAgent.java:2643 — the bus-key/state-key coupling is the point of this change but nothing asserts it; worth one case in HarnessAgentIdTest so a later refactor back to the raw field doesn't pass silently.
  • [Info] agentscope-harness/.../HarnessAgent.java:2398 — the name fallback still reaches the state dir / namespace unsanitised while an explicit id is format-checked. Documented as intentional and not a regression; just noting the asymmetry.

Coordination note: this supersedes my earlier "re-check the build jobs before merging" caveat — both build jobs now report success at this head, and merge readiness stays with the maintainers.


Automated review by github-manager-bot

TeamsMiddleware teamsMw = new TeamsMiddleware(teamsModeClient, teamsModeContext);
if (messageBus != null) {
teamsMw.wireMessageBus(messageBus, agentId != null ? agentId : name);
teamsMw.wireMessageBus(messageBus, resolvedAgentId);

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 from the previous round: all three message-bus keys (teamsMw.wireMessageBus, wireTaskRepositoryMessageBus, subagentsMw.wireMessageBus) now take resolvedAgentId, so the wake-up key can no longer drift from the state-dir / namespace key. Since that coupling is the whole point of the change, it would be worth locking down with an assertion in HarnessAgentIdTest (bus key == state dir name) — a future refactor back to the raw field would otherwise pass CI. Non-blocking.

// message-bus wake-up keys). Note: the name fallback is not validated — name has
// never been constrained, and validating it here would break existing users.
String normalizedAgentId = AgentBase.normalizeAgentId(agentId);
String resolvedAgentId =

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] Good call documenting that the name fallback is intentionally not validated. One residual asymmetry: with an unset agentId and a name containing characters normalizeAgentId rejects (spaces, /), the state dir / namespace still gets the unsanitised name, and the explicit-id path gets the format guard. Purely a documentation/follow-up point — name was never constrained, so this is not a regression introduced here.

boolean invalid =
normalized.length() > MAX_AGENT_ID_LENGTH
|| !AGENT_ID_PATTERN.matcher(normalized).matches()
|| normalized.chars().allMatch(c -> c == '.');

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.

全点 / 超长两个分支无测试,建议补下

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.

已经补充。

* for the namespace key and the inner agent keeps its generated UUID. Format contract:
* {@link AgentBase#normalizeAgentId(String)}, validated at {@code build()} time.
*/
public Builder agentId(String agentId) {

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.

存量用户的 getAgentId() 从 UUID 变为自定义 id,PR body 的 "purely additive" 需修正

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.

已修正

* @param description Agent description
* @param hooks List of hooks for monitoring/intercepting execution
* @param agentId explicit agent id; see {@link #normalizeAgentId(String)}. Blank falls back
* to a random UUID. Should be unique among live agents.

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.

唯一性怎么保证?

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.

未设置时由框架生成随机 UUID;显式设置时 agentId 完全由开发者管理,唯一性由开发者自行保证(同 sessionId)。框架只做格式校验(normalizeAgentId)。
已加强补充 javadoc ,其中增加明确提示:默认无需设置,仅在外部管理 id 时才需传入,并保证其在存活 agent 中任何时候唯一(已进一步完善 ReActAgent.Builder#agentId 的 javadoc)。

…avadoc

Address review feedback:
- add tests for the all-dots and over-length agentId rejection branches
  in normalizeAgentId, including the 255-char boundary (accepted)
- clarify the agentId javadoc: no need to set an id by default (the
  framework generates a unique UUID); an explicit id is caller-managed
  and its uniqueness is not enforced by the framework
@tanzzj

tanzzj commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

LGTM

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

LGTM

@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-reviewed the new commit 3aad8c17 (docs + tests only, on top of e6815e27 which I had already approved). It closes both open review threads: the all-dots / over-length branches of normalizeAgentId now have explicit tests, and the agentId javadoc in ReActAgent.Builder, AgentBase and HarnessAgent.Builder now states plainly that uniqueness is the caller's responsibility and is not enforced by the framework. The PR body also no longer claims the change is "purely additive", which was the other outstanding concern.

Verdict: LGTM. CI is green (build ubuntu/windows, validate, module sync, CLA signed) and mergeable is CLEAN. Two non-blocking notes below about the test mirroring a private constant and asserting on exception-message text.


Automated review by github-manager-bot

class AgentBaseTest {

/** Mirrors AgentBase's private MAX_AGENT_ID_LENGTH, documented on normalizeAgentId. */
private static final int MAX_AGENT_ID_LENGTH = 255;

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] This test-side MAX_AGENT_ID_LENGTH = 255 mirrors AgentBase's private constant. If the production limit is ever retuned, these tests will fail with a confusing message-comparison error rather than pointing at the constant. Consider having AgentBase expose the limit (e.g. a package-private constant or a public getter) and referencing it here, so the tests track the real bound.

"All-dots agent id '" + allDots + "' should be rejected");
assertTrue(
exception.getMessage().contains("all dots"),
"Error message should explain the all-dots rule");

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] Both new assertions couple to the exact wording/numbering of the IllegalArgumentException message ("all dots", "255"). That is fine as documentation-as-test, but a message rephrase will break them. If you would rather decouple, assert on the exception type plus a stable marker (or extract the message fragments into constants in AgentBase). Non-blocking.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants