Skip to content

[Bug]:CompositeFilesystem.sessionStorage() 硬编码 routeForPath("agents/"),Native Session Log 永远写不进 RemoteFilesystemSpec 的 sessions 路由 #4140

Closed

Description

Describe the bug / 问题描述

CompositeFilesystem.sessionStorage(RuntimeContext) 用一个硬编码的单段路径决定存储后端:

@Override
public AtomicSessionStorage sessionStorage(RuntimeContext rc) {
    return routeForPath("agents/").backend().sessionStorage(rc);
}
但 RemoteFilesystemSpec 注册的会话路由是 agents/{agentId}/sessions/(开启 IsolationScope.USER 时更深一层:agents/{agentId}/users/{userId}/sessions/)。

routeForPath 的匹配规则是「已注册前缀必须是查询路径的前缀」,即查询路径要以路由前缀开头。"agents/" 不可能以 "agents/<agentId>/sessions/" 开头,因此永远匹配不到,直接回落到 defaultBackend(本地文件系统 / SQLite),与路由如何注册无关。

后果:配置了 JDBC / 远程 BaseStore + RemoteFilesystemSpec 时,Native Session Log 被静默写到本地工作区,agentscope_store 一行都没有——没有异常,也没有 WARN。多副本部署下 Pod 一回收,会话历史直接消失。

这与 #2918(transcript segment key 与路由不匹配)、#2693(pruneOldSessions glob 与路由前缀不匹配)属于同一类「前缀层级不匹配」问题,但是另一个调用点:#2918 / PR #3386 只改 SessionTranscriptWriter,sessionStorage() 没有被修到。

To Reproduce / 复现步骤

DistributedStore store = JdbcDistributedStore.create(dataSource);
按官方文档的远程装配方式构建 Agent:
java


HarnessAgent.builder()
    .distributedStore(store)
    .filesystem(new RemoteFilesystemSpec()
        .store(store.baseStore())
        .isolationScope(IsolationScope.USER))
    .build();
跑几轮 agent.session(ctx).submit(...)。
观察:
sql


SELECT COUNT(*) FROM agentscope_store;   -- 返回 0
同时 journal 文件出现在本地工作区 / .agentscope-runtime/ 下。

Expected behavior / 期望行为

Session Log 应写入挂载在 agents/{agentId}/.../sessions/ 的存储命名空间,例如 agents/<agentId>/users/<userId>/sessions/__agentscope_session_log_v1__。

Error messages / 实际行为

没有异常、没有 WARN,静默回落到 defaultBackend,分布式存储中没有任何数据。这是最容易被忽略的地方——配置看起来完全正确。

Workaround / 临时绕过

不使用 Composite 分发,直接把远程文件系统整体绑上:

java


builder.abstractFilesystem(
    new RemoteFilesystem(
        store.baseStore(),
        ctx -> List.of("agents", agentId, "users", userId, "sessions")));
这样数据能正常写进 agentscope_store(已端到端验证)。代价是失去 IsolationScope 与默认多路由装配,namespace 需要应用自己维护。

Possible fix / 建议修复

让 sessionStorage 走和其他 CompositeFilesystem 操作同一张路由表,而不是固定的 "agents/":例如从 RuntimeContext 取到 agentId 后查询 agents/{agentId}/.../sessions/,或显式匹配以 agents/ 为根的路由。另外建议加防御性检查(解析出的后端是否真的支持 AtomicSessionStorage),不满足就明确报错,避免静默回落本地。

Environment / 环境

AgentScope-Java: 2.1.0-BETA1(main 上同样存在,2026-11-12 核对)
Java 21 / Spring Boot 3.5.x
Store: agentscope-extensions-jdbc 的 JdbcDistributedStore + MySQL 5.7
OS: Windows 11
Related / 相关

#2918 — transcript segment key 与 agents/{agentId}/sessions/ 路由不匹配(open)
#3386 — 修复 #2918 的 PR(open,且只覆盖 transcript 写入)
#2693 — pruneOldSessions glob 与路由前缀不匹配(open)

Activity

  1. added
    bug
    on Nov 12, 2026
  2. iluv7 commented on Nov 12, 2026

    @iluv7
    Contributor

    复现了。用 InMemoryStore 配合 RemoteFilesystemSpec(USER 隔离)提交一条会话事件,本地日志序号是 1,远程 sessions namespace 里还是 0,本地也确实生成了 .agentscope-runtime journal。绕过 Composite、直接用同一个 store 的 RemoteFilesystem 写入则正常,问题就在这里的后端选择。

    我想在装配 CompositeFilesystem 时显式传入 sessions 后端,让 sessionStorage(rc) 直接委托给它,文件操作继续走原来的路由表。RemoteFilesystemSpec 里已经创建了 sessions 路由,可以复用同一个后端,用户隔离也继续交给现有 namespace factory 处理。

    这样不用再从 RuntimeContext 补传 agentId,也能兼顾没有具体 SessionKey 的 list()。现有构造器保留兼容,再加一个参数版本给远程装配使用。

  3. aiyili commented on Nov 15, 2026

    @aiyili
    Contributor

    I've traced this and would like to take it. Verified on current main: CompositeFilesystem.sessionStorage() picks the backend via routeForPath("agents/"), but prefix matching requires the query path to start with the route prefix — and the registered session route is agents/{agentId}/sessions/ (deeper with IsolationScope.USER). "agents/" can never start with that, so the call always falls through to the default backend: Native Session Log silently lands in the local workspace while agentscope_store stays empty. Same "prefix-hierarchy mismatch" family as #2918/#2693, but a call site PR #3386 didn't touch.

    Proposed fix, aligned with @iluv7's reproduced analysis: RemoteFilesystemSpec already builds the sessions backend when it registers the route — expose it (or the composed route entry) and have CompositeFilesystem carry an explicit sessionStorageBackend that sessionStorage(rc) delegates to, instead of inferring a backend from a synthetic single-segment path. File operations keep using the route table; user isolation stays with the existing namespace factory. Will include a regression that submits a session event through the spec-composed composite and asserts the store receives it (local journal absent).

  4. iluv7 commented on Nov 15, 2026

    @iluv7
    Contributor

    @aiyili Thanks for looking into this! I've already submitted #4146 with this approach, including regression tests for remote persistence, recovery with a different local workspace, isolation, and history discovery. Would appreciate your review there so we can avoid duplicating the work.

  5. added a commit that references this issue on Nov 16, 2026
    5525ac1
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions