Skip to content

[Bug]:read_file/list_files 无法读取应用服务写入工作区根目录的文件:default backend 命名空间前缀导致读写不对称 #3245

Closed

Description

@wxh2010zd

问题概述

当宿主应用直接往工作区根目录写文件时(例如通过原生 java.nio 写入 {workspace}/uploads/{sessionId}/result.md),agent 的文件系统工具(read_file、list_files、grep_files)读不到这些文件:相对路径被解析到了 per-user 命名空间目录({workspace}/{userId}/uploads/...),而不是共享的工作区根目录。模型会因此浪费几十轮尝试各种路径变体,最终任务失败。远程模式(RemoteFilesystemSpec)和本地模式(LocalFilesystemSpec)都会中招,Windows 和 Linux 表现一致。

环境

  • io.agentscope:agentscope-harness:2.0.3-SNAPSHOT

  • 本地模式,Windows 11(已复现);远程模式 + Redis 存储,Linux(同一代码路径)

  • 默认隔离范围(本地模式未覆盖时默认为 USER)

观测到的现象(来自真实运行日志)

工作区根:C:\opt\.agentscope\workspace-business
应用侧工具将识别结果写入 {workspace}\uploads\ff17dbe6\扫描0090.md(724 字节,磁盘上确认存在,且 glob_files 能看到):

  • read_file("uploads/ff17dbe6/扫描0090.md") → not found

  • read_file("C:\opt\.agentscope\workspace-business\uploads\ff17dbe6\扫描0090.md") → not found

  • list_files("uploads") → "Path does not exist"

  • glob_files(path="C:\opt") → 返回 uploads/ff17dbe6/扫描0090.md

随后模型循环尝试了 10 多种路径变体(相对路径、绝对路径、正斜杠、./、../)——全部失败。

根因分析

  1. RemoteFilesystemSpec.toFilesystem(...) 构建 default backend(即所有不匹配共享路由的路径——包括 uploads/——都由它服务)时:

    new LocalFilesystem(workspace, false /*sandboxed*/, 10, localNamespaceFactory)

    即 UNRESTRICTED 模式 + 用户命名空间工厂(IsolationScope.USER → [userId])。

  2. LocalFilesystem.applyNamespacePrefix 会给相对路径加命名空间前缀(uploads/x.md → {userId}/uploads/x.md);绝对路径则原样放行。

  3. 宿主应用用原生 java.nio 写入 {workspace}/uploads/...(无命名空间),而工具把同一个逻辑路径解析为 {workspace}/{userId}/uploads/... → 文件不存在。写入侧和读取侧对物理位置的认知不一致。

  4. WorkspacePathNormalizer.normalize 会把绝对路径中的工作区前缀剥掉,使其变成相对路径,随后同样被加上命名空间前缀——所以绝对路径变体也失败。

  5. 工作区之外的绝对路径(例如 C:\opt)既不经过 normalizer 剥离、也不加命名空间前缀——这就是 glob_files(path="C:\opt") 能看到文件而 read_file 看不到的原因。另外注意:glob 的返回结果是相对于固定虚拟根(工作区)的,而不是相对于 path 参数的,因此 glob 与 read/list 的根基准彼此也不一致。

  6. 本地模式存在同样的不对称:LocalFilesystemSpec.toFilesystem 把命名空间工厂传给了 upper 文件系统层,因此默认本地配置下工具看到的是 {workspace}/{userId}/...,而应用写的是 {workspace}/...(工作区内出现 default-user/ 目录可佐证)。

综合影响:任何"应用写入工作区根 → agent 读取"形态的集成(用户文件上传是最典型的场景)都会无声失效,且失败模式是模型在路径变体上空转、大量消耗 token。

安全附带问题

由于 default backend 是 UNRESTESTRICTED 模式,本地配置下 list_files(".") 可以一路向上遍历到盘根(实测会列出 C:\)。如果把共享目录的 backend 换成 SANDBOXED 模式,可以同时解决可见性不对称和这个路径遍历问题。

建议修复方案(任选其一)

  1. 在 RemoteFilesystemSpec 上增加一个可选开关,例如 sharedLocalWorkspace(true),将 default backend 构建为 new LocalFilesystem(workspace, true /*sandboxed*/, 10, null)——共享工作区根、不加命名空间前缀、工作区外的绝对路径抛 SecurityException。共享路由(memory/、skills/、agents/{id}/sessions|tasks/ 等)的 per-user 命名空间语义保持不变。

  2. 或者在 LocalFilesystem.applyNamespacePrefix 中支持命名空间排除前缀列表(例如 uploads/),让指定子树解析到共享工作区根,其余路径保持命名空间隔离。

Activity

  1. added
    bug
    on Sep 22, 2026
  2. aiyili commented on Sep 22, 2026

    @aiyili
    Contributor

    Hi, I'd like to work on this issue, following suggested fix 1 (extended to the local mode, per point 6):

    1. RemoteFilesystemSpec.sharedLocalWorkspace(true) (opt-in, default false preserves current behavior): the default backend becomes new LocalFilesystem(workspace, true /*sandboxed*/, 10, null) — the same construction already used for the workspace-template layer. Host-app files written to the workspace root with plain java.nio (e.g. uploads/) become visible to read_file/list_files/grep_files, and absolute paths outside the workspace are rejected instead of traversing up to the drive root (the security side-note). Shared routes (memory/, skills/, ...) keep their per-user store namespaces.
    2. Same flag on LocalFilesystemSpec (the report shows local mode has the identical read/write asymmetry): pass a null namespace factory to the upper/project layers when enabled.
    3. Tests: host-written file at the workspace root round-trips through read/ls; flag-off default keeps the namespaced behavior (regression guard); sandboxed backend rejects an outside-workspace absolute path; shared routes stay isolated per user.

    PR coming shortly.

  3. added
    area/harness
    on Sep 22, 2026
  4. oss-maintainer commented on Sep 22, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Module: agentscope-harness | Status: Confirmed (verified against main @ ed77530a4)

    Root cause — each claim in the report was checked against the code:

    1. RemoteFilesystemSpec.toFilesystem builds the default backend as new LocalFilesystem(workspace, false, 10, localNamespaceFactory) (.../filesystem/spec/RemoteFilesystemSpec.java:212), where false selects the UNRESTRICTED mode (LocalFilesystem.java:149-155). Confirmed.
    2. LocalFilesystem.applyNamespacePrefix (LocalFilesystem.java:689-707) prefixes relative paths only — isAbsolutePathString (:714-727) passes absolute paths through, and WorkspacePathNormalizer.normalize (WorkspacePathNormalizer.java:119-144) strips the workspace prefix before resolution, which turns an absolute path back into a relative one that then receives the prefix. Confirmed — this is exactly why the absolute-path variants in the log also fail.
    3. Read/write asymmetry: under the default IsolationScope.USER mapping (IsolationScope.java:102-126, USER -> [userId]), read_file("uploads/x.md") resolves to {workspace}/{userId}/uploads/x.md while a host java.nio write lands at {workspace}/uploads/x.md. There is no code path under default settings where a host write to the workspace root is readable by the tools (the two coincide only when the namespace is empty, i.e. AGENT/GLOBAL or a blank user/session key). Confirmed.
    4. glob_files vs read_file/list_files root mismatch: LocalFilesystem.glob resolves against cwd/resolvePath(".") (LocalFilesystem.java:421-425) while CompositeFilesystem.ls maps "." to defaultBackend.ls("/") (CompositeFilesystem.java:199-201), which UNRESTRICTED mode passes through unchanged (LocalFilesystem.java:681-687). Confirmed — and it also confirms the security side-effect: list_files(".") can enumerate outside the workspace (drive root), since no containment check exists on that path.
    5. Local mode: LocalFilesystemSpec.toFilesystem passes the same namespace factory to LocalFilesystemWithShell (LocalFilesystemSpec.java:288-320), so the asymmetry is identical there. Confirmed.

    Impact: any "host application writes into the workspace root → agent reads it" integration (file uploads being the canonical case) fails silently, and the failure mode is expensive: the model burns dozens of turns on path variants. The unrestricted default listing path additionally weakens workspace containment.

    Severity: high.

    Fix direction — needs a design decision before any PR. Two variants are on the table:

    • (opt-in flag) add sharedLocalWorkspace(true) on the specs so the default backend becomes new LocalFilesystem(workspace, true, 10, null);
    • (targeted) a namespace-exclusion prefix list in LocalFilesystem.applyNamespacePrefix (e.g. uploads/), so only selected subtrees resolve to the shared root.

    The second option has the smaller change surface and keeps per-user isolation for every non-excluded path; the first makes the shared-root behaviour all-or-nothing. Whichever is chosen, these must not regress: shared routes (memory/, skills/, agents/<id>/sessions|tasks/) keep their per-user namespaces, and per-user isolation for ordinary workspace files stays the default.

    Note for the reporter: PR #3247 (opened 2026-09-22T03:56Z, Fixes #3245) already implements the first option. This comment is not an endorsement of that PR; the module-maintainer API choice above should be settled first, and any fix should also cover the traversal containment in point 4, which is out of scope of #3247 as written.


    Automated evaluation by github-manager-bot

  5. aiyili commented on Sep 22, 2026

    @aiyili
    Contributor

    Thanks for the thorough evaluation. On the two open points:

    PR #3247 status (option 1) — opt-in sharedLocalWorkspace(true) on both specs, default behavior unchanged (namespaced, UNRESTRICTED), shared routes keep their per-user store namespaces, traversal blocked when enabled (sandboxed backend). The review items on the PR have been addressed. If the module maintainer prefers option 2 (namespace-exclusion prefix list in applyNamespacePrefix), I'm happy to rework the PR in that direction — the tests would transfer mostly as-is.

    Point 4 containment gap (option-independent) — agreed this needs fixing regardless of the API choice. Concrete mechanism, verified against the code: CompositeFilesystem.ls routes "/"/"." to defaultBackend.ls(rc, "/") (CompositeFilesystem.java:199-203), and the UNRESTRICTED resolver passes absolute paths through unchanged (LocalFilesystem.java:681-687), so the listing enumerates from the OS root. A minimal fix that works under both options: anchor the composite's root listing to the backend's own root — pass a blank path (which resolvePath already resolves to the backend cwd, LocalFilesystem.java:598-600) instead of "/" — or equivalently teach LocalFilesystem that "/" means the backend root rather than the OS root in every mode. The first is a one-line change in CompositeFilesystem plus a regression test; the second is broader but makes /-semantics consistent everywhere.

    Happy to push the containment fix into #3247 or as a separate PR, whichever the maintainer prefers — awaiting the API decision.

  6. oss-maintainer commented on Sep 22, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation — follow-up (design question resolved; no action needed from the bot)

    Category: bug | Module: agentscope-harness | Status: Confirmed earlier; the open API decision is now settled — leaving implementation to PR #3247 (re-checked against main @ ed77530a4)

    • API choice: PR feat(harness): opt-in sharedLocalWorkspace on the filesystem specs (host-app workspace-root asymmetry) #3247 (option 1, opt-in sharedLocalWorkspace(true)) has been reviewed and approved by a maintainer (approval recorded 2026-09-22T05:39Z, mergeable, non-draft), and the reporter is asking for a merge. The option-2 alternative raised above is therefore superseded — the bot will not generate a competing patch for this issue and will not re-open the design question.
    • Point 4 (root-listing containment) is still unaddressed on main. Verified again at HEAD: CompositeFilesystem.ls still routes the root listing to the backend with an absolute "/" — if ("/".equals(path) || ".".equals(path)) { ... defaultBackend.ls(runtimeContext, "/"); ... } (agentscope-harness/src/main/java/io/agentscope/harness/agent/filesystem/CompositeFilesystem.java:199-201), and the UNRESTRICTED resolver passes absolute paths through unchanged (LocalFilesystem.java:681-687). @aiyili's proposed minimal fix (pass a blank path so resolvePath anchors at the backend root, LocalFilesystem.java:598-600) is consistent with the code as it stands; note the same branch also feeds . listings for shared routes, so whichever way it lands it needs a regression test that a .// listing cannot escape the workspace in both spec modes, and that per-user namespace isolation for ordinary paths is unchanged.
    • Coverage check on feat(harness): opt-in sharedLocalWorkspace on the filesystem specs (host-app workspace-root asymmetry) #3247 passed: it touches both specs, so the local-mode twin of the asymmetry is not left behind — LocalFilesystemSpec.java (+37/-3), RemoteFilesystemSpec.java (+36/-1), plus LocalFilesystemSpecSharedWorkspaceTest.java (+96) and RemoteFilesystemSpecTest.java (+64). Only the point-4 containment fix above remains outside its scope.

    Action taken this tick: none on GitHub beyond this status update — no new label, no bot PR, no comment that would add noise to an approved-and-awaiting-merge thread. If #3247 merges, this issue can be closed by the maintainers with the containment follow-up tracked separately.


    Automated evaluation by github-manager-bot

  7. gmjneko commented on Sep 22, 2026

    @gmjneko
    Contributor

    refer #1775

  8. oss-maintainer commented on Sep 22, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation — follow-up: the "#1775" reference is not a duplicate

    Category: bug | Module: agentscope-harness | Status: unchanged (Confirmed; fix tracked in #3247) — evaluated the cross-reference raised above against main @ ed77530a4

    Checked #1775 and it is a separate defect, not an earlier report of this one — see the full analysis in #1775. Short version: #1775 lives in the local project overlay, where LocalFilesystemSpec builds the writable project layer with the namespace factory (.../filesystem/spec/LocalFilesystemSpec.java:309-311) but the read-fallback layer without it (:307, new LocalFilesystem(effectiveProject, true, 10, null)), so a framework write and the matching read resolve to different physical paths. This issue is about the remote default backend namespacing tool reads while a host java.nio write lands un-namespaced (.../filesystem/spec/RemoteFilesystemSpec.java:212).

    Consequence for this thread: PR #3247 will not close #1775, and #1775 should stay open. No change to the status of this issue, and no bot action here beyond this note.


    Automated evaluation by github-manager-bot

  9. added a commit that references this issue on Sep 24, 2026
    320822d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/harnessbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions