agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]: AgentScope-Java V2 版本中手动设置agentId后,获取到的agentId还是uuid格式

Open ethan-lee-club opened this issue 5 months ago • 4 comments

package com.eavision.agent;

import io.agentscope.core.agent.RuntimeContext;
import io.agentscope.core.message.Msg;
import io.agentscope.core.message.MsgRole;
import io.agentscope.core.tool.Toolkit;
import io.agentscope.harness.agent.HarnessAgent;
import io.agentscope.harness.agent.filesystem.spec.LocalFilesystemSpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

/**
 * 验证 HarnessAgent 的 agentId 设置和获取。
 */
public class AgentIdVerificationDemo {
    private static final Logger LOGGER = LoggerFactory.getLogger(AgentIdVerificationDemo.class);

    public static void main(String[] args) throws IOException {
        System.out.println("=== HarnessAgent AgentId 验证 Demo ===\n");

        // 手动指定 agentId
        String manualAgentId = "alice:note-taker";
        System.out.println("1. 手动指定 agentId: " + manualAgentId);

        // 构建 HarnessAgent
        HarnessAgent agent = HarnessAgent.builder()
                .name("note-taker")
                .agentId(manualAgentId)
                .sysPrompt("You are a helpful assistant.")
                .model("dashscope:qwen3.6-plus")
                .workspace(Path.of(System.getProperty("user.home"), ".harness-agent", "workspaces", "alice", "agents", "note-taker"))
                .toolkit(new Toolkit())
                .filesystem(new LocalFilesystemSpec())
                .build();

        // 获取 agentId
        String retrievedAgentId = agent.getAgentId();
        System.out.println("2. 获取 agent.getAgentId(): " + retrievedAgentId);

        // 验证是否一致
        boolean isSame = manualAgentId.equals(retrievedAgentId);
        System.out.println("3. 验证结果: " + (isSame ? "✅ 一致" : "❌ 不一致"));

        if (!isSame) {
            System.out.println("   期望值: " + manualAgentId);
            System.out.println("   实际值: " + retrievedAgentId);
        }

        agent.close();
        System.out.println("\n=== Demo 结束 ===");
    }
}

17:39:18.433 [main] INFO io.agentscope.core.skill.repository.FileSystemSkillRepository -- FileSystemSkillRepository initialized with base directory: /Users/lxw/.harness-agent/workspaces/alice/agents/note-taker/skills
17:39:18.442 [main] INFO io.agentscope.harness.agent.tools.ToolFilter -- tools.json filter applied: removed 12 tool(s); 6 tool(s) remain: [edit_file, glob_files, grep_files, list_files, read_file, write_file]
17:39:18.442 [main] INFO io.agentscope.harness.agent.HarnessAgent -- HarnessAgent 'note-taker' built [workspace=/Users/lxw/.harness-agent/workspaces/alice/agents/note-taker, backend=ShellAwareOverlay, subagents=true]
2. 获取 agent.getAgentId(): 86882652-25d0-4644-9109-e51495714ed6
3. 验证结果: ❌ 不一致
   期望值: alice:note-taker
   实际值: 86882652-25d0-4644-9109-e51495714ed6

=== Demo 结束 ===

ethan-lee-club avatar Jun 03 '26 09:06 ethan-lee-club

I’m very interested in this issue and would appreciate the opportunity to help fix it.

After tracing the construction path, I believe the problem is not that HarnessAgent#getAgentId() fails to forward the Builder value to the internal ReActAgent; rather, the current code uses two identifiers with different semantics:

  • The Javadoc for HarnessAgent.Builder.agentId(...) defines it as the stable namespace key for the composite filesystem, used for Harness resource isolation such as workspaces, default state directories, and sandboxes.
  • Agent#getAgentId() is documented as the unique identifier for an agent. HarnessAgent#getAgentId() currently delegates to the internal ReActAgent, whose AgentBase generates a UUID for each runtime instance.

Forwarding the Harness agentId directly to ReActAgent would conflate a stable resource namespace with a unique runtime instance identity. For example, a logical agent may intentionally retain the same namespace across restarts or replicas, while each ReActAgent instance should still have its own unique ID. Such forwarding would change the existing Agent#getAgentId() contract and require callers to guarantee global uniqueness for custom values.

I suggest preserving the separation of concerns:

  1. Keep HarnessAgent#getAgentId() delegating to delegate.getAgentId() so it continues to return the actual ReActAgent instance ID.
  2. Add a clearly named namespaceId(...) Builder API for configuring the Harness workspace, default state directory, and sandbox namespace.
  3. Deprecate the current ambiguous agentId(...) Builder method and retain it as a compatibility alias that delegates to namespaceId(...).
  4. Update the relevant Javadocs, examples, and migration notes to clearly distinguish the unique instance identity returned by Agent#getAgentId() from the stable Harness resource namespace represented by namespaceId.

This approach removes the API ambiguity, preserves the Core agent identity contract, and provides a smooth migration path for existing users. If maintainers agree with this direction, I would be glad to take responsibility for the implementation and tests.

CyberXavier avatar Jul 10 '26 09:07 CyberXavier

@ethan-lee-club @AgentScopeJavaBot

CyberXavier avatar Jul 15 '26 07:07 CyberXavier

应该是不行的,底层代码在AgentBase里面是直接构造函数赋值的,并没有提供外部修改的接口哦。 算是官方的设计问题吧。

TianyuGeek001 avatar Jul 20 '26 01:07 TianyuGeek001

Fixed in: https://github.com/agentscope-ai/agentscope-java/pull/3360

jujn avatar Oct 08 '26 13:10 jujn