Repository navigation
feat(core): allow setting a custom agentId via builder, falling back to a random UUID when absent - #3360
Conversation
…m UUID when absent
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…arness state dirs
…idate at build time
oss-maintainer
left a comment
There was a problem hiding this comment.
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 inHarnessAgentIdTestso a later refactor back to the raw field doesn't pass silently. - [Info]
agentscope-harness/.../HarnessAgent.java:2398— thenamefallback 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); |
There was a problem hiding this comment.
[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 = |
There was a problem hiding this comment.
[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 == '.'); |
| * 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) { |
There was a problem hiding this comment.
存量用户的 getAgentId() 从 UUID 变为自定义 id,PR body 的 "purely additive" 需修正
| * @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. |
There was a problem hiding this comment.
未设置时由框架生成随机 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
|
LGTM |
oss-maintainer
left a comment
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
[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"); |
There was a problem hiding this comment.
[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.
What
Adds an opt-in
agentIdtoReActAgent.builder():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
HarnessAgentusers:HarnessAgent.Builder.agentId(...)is now forwarded to the inner agent, sogetAgentId()returns the configured id instead of the inner agent's generated UUID.Changes
AgentBase: new constructor overload(name, description, hooks, agentId); null/blankfalls back to
UUID.randomUUID(). Existing constructors delegate withnull.ReActAgent.Builder: newagentIdfield + setter.ReActAgent: passesbuilder.agentIdtoAgentBase.Relationship to #2395
Same goal as #2395, but as a minimal change: reuses the existing
agentId/getAgentId()instead of introducing an additionalidproperty. Feel free to mergeeither one and close the other.