[Bug]:AI创建的文件实际路径和返回给用户看到的不一致
场景:
我让AI给我创建一个文件,然后确实也创建了。说创建路径在 D:\Java\agentscopeWorkSpace\hello_world.py 实际上根本没有,而是生成到了 D:\Java\agentscopeWorkSpace\web-user\hello_world.py
AI分析如下:
这是 AgentScope 框架的一个路径报告 Bug,不是你的使用方式有问题。核心原因有两个:
1. 用户命名空间隔离把文件写到了 web-user/ 下
你的 AgentChatService.chatStream() 中,当请求没有带 userId 时,默认设为 "web-user":
if (userId == null || userId.isBlank()) {
userId = "web-user";
}
RuntimeContext ctx = RuntimeContext.builder()
.sessionId(sid)
.userId(uid)
.build();
然后 AgentScope 的 HarnessAgent 默认使用 IsolationScope.USER 做文件系统隔离(见 IsolationScope.java)。它的 NamespaceFactory 会把所有相对路径前加上 userId:
case USER -> rc -> {
String uid = rc.getUserId();
if (uid != null && !uid.isBlank()) {
return List.of(uid); // 这里就是 "web-user"
}
...
};
所以在 LocalFilesystem 内部,hello_world.py 被解析成 web-user/hello_world.py,实际写入:
D:\Java\agentscopeWorkSpace\web-user\hello_world.py
2. 但工具返回给 AI 的路径没有包含这个前缀(Bug 所在)
真正的问题在 AgentScope 源码的 LocalFilesystem.write():
@Override
public WriteResult write(RuntimeContext runtimeContext, String filePath, String content) {
Path resolved = resolvePath(runtimeContext, filePath); // 已解析为 web-user/hello_world.py
...
Files.writeString(resolved, content, StandardCharsets.UTF_8);
return WriteResult.ok(filePath); // ❌ 这里返回的是原始的 "hello_world.py"
}
它写的是解析后的路径,但返回的是原始逻辑路径。而 WriteResult 的注释明确说 path 应该是 absolute path of written file(写入文件的绝对路径)。
FilesystemTool.writeFile() 又把这个错误路径原样返回给 AI:
return r.isSuccess() ? "Written to " + r.path() : "Error: " + r.error();
AI 拿到 "hello_world.py" 后,再自己拼成 D:\Java\agentscopeWorkSpace\hello_world.py 告诉你,就造成了你看到的“路径对不上”。
现在你的文件在哪
真实位置是:
D:\Java\agentscopeWorkSpace\web-user\hello_world.py
怎么解决
有三种办法,按推荐程度排列:
方案 A:修正 AgentScope 框架源码(最彻底)
如果你本地会重新编译 agentscope-java,把 LocalFilesystem.java 里的 write() 改成返回解析后的路径:
return WriteResult.ok(resolved.toString());
同样建议检查 edit()、delete()、move()、uploadFiles(),它们也有类似问题(返回原始 path 而不是 resolved path)。
I'd like to work on this. Plan: make LocalFilesystem write/edit/delete/move return the resolved absolute path in WriteResult/EditResult (per the record javadoc), so tool replies report the real on-disk location under namespace isolation. Will add a focused unit test.
This is a valid bug. The file is created at D:\Java\agentscopeWorkSpace\web-user\hello_world.py but the reported path is D:\Java\agentscopeWorkSpace\hello_world.py. The user sees the wrong path.
Root cause: Two issues combine:
- USER namespace isolation prepends
web-user/to the actual file path, butLocalFilesystem.write()returns the original (non-namespaced) path in its result. - Path reporting mismatch — the write result should return the actual path where the file was created, not the logical path the user requested.
Related: This is the same class of bug as #2298 (agentName vs agentId path mismatch in session files). Both stem from the namespace layer silently modifying paths without updating the return values.
Suggested fix: LocalFilesystem.write() (and edit()) should return the resolved physical path in the WriteResult, not the logical input path. This ensures the user/agent always sees where files actually are.