agentscope-ai / agentscope-ai/agentscope-java

[Bug]:AI创建的文件实际路径和返回给用户看到的不一致

Aberta
#2,266 2 comentários 0 reações 0 responsáveis Ver no GitHub
area/harness bug
Linguagem predominante
Java
Estrelas
5.6k
Forks
1.3k
Merge médio
4d 12h
PRs com merge (30d)
77

Descrição

场景:
我让AI给我创建一个文件,然后确实也创建了。说创建路径在 D:\Java\agentscopeWorkSpace\hello_world.py 实际上根本没有,而是生成到了 D:\Java\agentscopeWorkSpace\web-user\hello_world.py
Image

Image
问了AI分析原因,说是框架的 LocalFilesystem.write() 方法返回存在Bug导致。

AI分析如下:

这是 **AgentScope 框架的一个路径报告 Bug**,不是你的使用方式有问题。核心原因有两个:

## 1. 用户命名空间隔离把文件写到了 `web-user/` 下

你的 [`AgentChatService.chatStream()`](file:///D:/Java/Project2/agentScopeDemo/src/main/java/com/example/myagentdemo/service/AgentChatService.java#L75-L92) 中,当请求没有带 `userId` 时,默认设为 `"web-user"`:

```java
if (userId == null || userId.isBlank()) {
userId = "web-user";
}
RuntimeContext ctx = RuntimeContext.builder()
.sessionId(sid)
.userId(uid)
.build();
```

然后 AgentScope 的 `HarnessAgent` 默认使用 **`IsolationScope.USER`** 做文件系统隔离(见 [`IsolationScope.java`](file:///D:/Java/agentscope-java/agentscope-harness/src/main/java/io/agentscope/harness/agent/IsolationScope.java#L71-L115))。它的 `NamespaceFactory` 会把所有相对路径前加上 `userId`:

```java
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`,实际写入:

```text
D:\Java\agentscopeWorkSpace\web-user\hello_world.py
```

## 2. 但工具返回给 AI 的路径没有包含这个前缀(Bug 所在)

真正的问题在 AgentScope 源码的 [`LocalFilesystem.write()`](file:///D:/Java/agentscope-java/agentscope-harness/src/main/java/io/agentscope/harness/agent/filesystem/local/LocalFilesystem.java#L322-L342):

```java
@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()`](file:///D:/Java/agentscope-java/agentscope-harness/src/main/java/io/agentscope/harness/agent/tool/FilesystemTool.java#L82-L88) 又把这个错误路径原样返回给 AI:

```java
return r.isSuccess() ? "Written to " + r.path() : "Error: " + r.error();
```

AI 拿到 `"hello_world.py"` 后,再自己拼成 `D:\Java\agentscopeWorkSpace\hello_world.py` 告诉你,就造成了你看到的“路径对不上”。

## 现在你的文件在哪

真实位置是:

```text
D:\Java\agentscopeWorkSpace\web-user\hello_world.py
```

## 怎么解决

有三种办法,按推荐程度排列:

### 方案 A:修正 AgentScope 框架源码(最彻底)

如果你本地会重新编译 `agentscope-java`,把 [`LocalFilesystem.java`](file:///D:/Java/agentscope-java/agentscope-harness/src/main/java/io/agentscope/harness/agent/filesystem/local/LocalFilesystem.java#L322-L342) 里的 `write()` 改成返回解析后的路径:

```java
return WriteResult.ok(resolved.toString());
```

同样建议检查 `edit()`、`delete()`、`move()`、`uploadFiles()`,它们也有类似问题(返回原始 path 而不是 resolved path)。

Guia de contribuição

Abrir o guia de contribuição

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.