Skip to content

[Bug]: 当前 main 默认 ObjectStoreTranscriptStore 的 segment key 不匹配 RemoteFilesystemSpec 路由,session 历史无法写入远程/MySQL #2918

Open

Description

@allendata0706

环境

agentscope-harness v2.0.2-95-gb4361103(当前 main)
store agentscope-extensions-mysql(JdbcStore → MySQL 8.0)
filesystem RemoteFilesystemSpec with IsolationScope.USER
场景 非 sandbox,CompositeFilesystem + 远程 store

问题

在 v2.0.2 中,session JSONL 会走 MemoryFlushMiddleware → offloadMessages → SessionTree.scheduleMirror(),最终通过 RemoteFilesystem.uploadFiles() 写入 MySQL 的 agentscope_store 表。

当前 main 分支引入了 TranscriptStore 体系,默认使用 ObjectStoreTranscriptStore:

// HarnessAgent.java ~L2410
if (effectiveTranscriptStore == null) {
    if (wsManager.getFilesystem() != null) {
        effectiveTranscriptStore = new ObjectStoreTranscriptStore(wsManager.getFilesystem());
    } else {
        effectiveTranscriptStore = new FilesystemTranscriptStore(...);
    }
}

ObjectStoreTranscriptStore.appendSegment() 生成的 key 为:

default/{agentId}/{sessionId}/events/{seqStart}-{seqEnd}-{writerId}.jsonl

而 RemoteFilesystemSpec 注册的路由是:

routes.put("agents/" + effectiveAgentId + "/sessions/", overlayRoute(...));

default/{agentId}/{sessionId}/... 与 agents/{agentId}/sessions/... 不匹配,因此 CompositeFilesystem.routeForPath() 会 fallback 到 defaultBackend(LocalFilesystem),session segment 只写本地 workspace,不进 RemoteFilesystem,不进 MySQL。

复现

HarnessAgent.builder()
    .agentId("agent-qa")
    .name("agent-qa")
    .distributedStore(new MysqlDistributedStore(dataSource))
    .filesystem(new RemoteFilesystemSpec())
    .build();

运行一次完整对话后:

SELECT * FROM agentscope_store WHERE namespace_path LIKE '%sessions%';
-- 预期:有 agents␟agent-qa␟users␟...␟sessions␟ 前缀的记录
-- 实际:没有 session 相关记录

本地 workspace 会出现:

workspace/<uid>/default/agent-qa/<sessionId>/events/0-5-xxx.jsonl

根因

  1. ObjectStoreTranscriptStore 的 segment key layout 是 {tenant}/{agentId}/{sessionId}/events/...,与 RemoteFilesystemSpec 的 {agents}/{agentId}/sessions/ 路由前缀不一致。
  2. 即使手动调整 key 前缀去匹配 agents/{agentId}/sessions/,USER scope 下还带有 users/{userId} 层级,而 TranscriptRef 中没有 userId,难以正确构造命中路由的 key。
  3. 框架目前没有内置基于 JDBC/MySQL 的 TranscriptStore 实现,用户无法通过 .transcriptStore(...) 直接接入已有 DataSource。

影响

  1. 升级到当前 main 后,原先 v2.0.2 能写入 MySQL 的 session 历史会"静默丢失"到本地盘。
  2. 在 K8s / emptyDir / 多副本场景下,本地 workspace 随 Pod 销毁,session 无法跨副本恢复。
  3. 用户配了 RemoteFilesystemSpec + JdbcStore 后,会误以为 session 已持久化到 MySQL,但实际上没有。

建议

任一即可:

  1. 官方提供 JdbcTranscriptStore / RdbmsTranscriptStore 实现
    直接通过 JDBC 同步写入独立表(如 agentscope_transcript_segments),不依赖 RemoteFilesystemSpec 路由匹配。

  2. 调整默认 ObjectStoreTranscriptStore 的 key layout 以匹配 RemoteFilesystemSpec 路由
    默认生成 agents/{agentId}/sessions/{sessionId}/events/... 或兼容 IsolationScope 的 key,并确保 TranscriptMiddleware 传入的 RuntimeContext 携带 userId 以支持 USER scope。

  3. 至少提供配置开关
    例如 .transcriptStore(TranscriptStore.legacyMirror()) 或 .disableTranscriptStore(),让用户能显式选择行为,避免静默降级到本地盘。

相关

Activity

  1. added
    bug
    on Aug 31, 2026
  2. CryoThrust commented on Aug 31, 2026

    @CryoThrust
    Contributor

    I would like to investigate this regression. The interaction between ObjectStoreTranscriptStore key layout, CompositeFilesystem routing, and USER-scope path construction looks important to preserve before proposing code. I plan to reproduce with a minimal in-memory backend first, then confirm whether the intended fix is a routing-compatible key layout or an explicit JDBC transcript store, without silently changing persistence semantics.

  3. CryoThrust commented on Sep 4, 2026

    @CryoThrust
    Contributor

    I traced the reported regression to a routing contract mismatch rather than a MySQL write failure:

    • ObjectStoreTranscriptStore emits segment keys under default/{agentId}/{sessionId}/events/....
    • RemoteFilesystemSpec routes session data under agents/{agentId}/sessions/ (and USER isolation adds the user namespace).
    • CompositeFilesystem therefore falls back to its local backend, so the transcript can be written successfully but never reach the configured remote store.

    Before preparing a patch, could maintainers confirm the intended ownership of transcript persistence?

    1. Should the default object-store transcript key layout be changed to the routed workspace layout, including an explicit user/session namespace?
    2. Or should transcript segments bypass filesystem routing through a dedicated JDBC/RDBMS TranscriptStore?
    3. For existing users upgrading from v2.0.2, is preserving the legacy key layout/read compatibility required?

    A focused regression test can assert that a RemoteFilesystemSpec-backed transcript write reaches the remote store and does not silently fall back to local storage. I can implement once the compatibility direction is confirmed.

  4. allendata0706 commented on Sep 4, 2026

    @allendata0706
    Author

    v2.0.2 中
    但 MemoryFlushMiddleware 和 CompactionMiddleware 使用String agentId = agent.getName() 构造 Session 路径:
    生成的路径 :agents/妙妙/sessions/.jsonl,session文件就无法卸载到远程文件系统里。
    应该使用 agentId = agentId.getAgentId(); 这样才可以写到远程文件系统中。见: #2297

    对于 master分支,ObjectStoreTranscriptStore 需要解决写入 mysql key 匹配问题。

  5. oss-maintainer commented on Sep 14, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Area: area/harness | Status: Confirmed (verified against current main)

    Root Cause: ObjectStoreTranscriptStore.appendSegment writes segment keys under default/{agentId}/{sessionId}/events/..., while RemoteFilesystemSpec only overlays paths matching agents/{agentId}/sessions/. CompositeFilesystem.routeForPath therefore falls back to the local default backend and session transcript segments never reach the remote (MySQL) store.
    Impact: Session history is silently not persisted with RemoteFilesystemSpec + IsolationScope.USER; migrating from the v2.0.2 SessionTree mirror path loses replayability.
    Severity: high

    A concise fix would align the transcript segment key layout with the RemoteFilesystemSpec routing prefix (or make the routing prefix configurable per store). An automated fix proposal can be generated on request.


    Automated evaluation by github-manager-bot

  6. added
    area/harness
    on Sep 14, 2026
  7. aiyili commented on Oct 1, 2026

    @aiyili
    Contributor

    Hi, I'd like to work on this. The segment-key format produced by ObjectStoreTranscriptStore.appendSegment doesn't match the routing prefixes RemoteFilesystemSpec registers, so transcript writes fall to the default backend instead of the shared agents/<id>/sessions/ route. I'll align the key layout (or add a route) with tests asserting writes land in the routed store namespace. PR coming shortly.

  8. aiyili commented on Nov 27, 2026

    @aiyili
    Contributor

    Picking this up again for the 2.0.x branch. The code that #3386 changed still exists there and the mismatch is still reproducible: on 2.0.x (50886798) ObjectStoreTranscriptStore.appendSegment still writes {tenant}/{agentId}/{sessionId}/events/... (default tenant default), which does not match the agents/{agentId}/sessions/ route that RemoteFilesystemSpec registers, so segments fall through to the default backend.

    Plan, as a PR against 2.0.x only. It is the final state of #3386, with the three items from the 10-02 review:

    1. Route-aligned keys for the default object-store transcript store (agents/{agentId}/sessions/{sessionId}), built from the builder's stable agent id, the one the routes are built with, threaded through TranscriptMiddleware into SessionTranscriptWriter.
    2. A routing regression test that waits on awaitMirrorQuiescence and covers the split case (agentId different from the display name).
    3. The transcriptTenant javadoc updated, and the behavior for segments written under the old key layout stated in the PR: they stayed on the local disk of the node that wrote them (that is the bug), the local context file remains the primary copy there, and they are not migrated.
  9. aiyili commented on Nov 27, 2026

    @aiyili
    Contributor

    Opened #4450 against 2.0.x.

    On unmodified 2.0.x a HarnessAgent built with RemoteFilesystemSpec leaves its transcript segments on the local disk: ObjectStoreTranscriptStore writes {tenant}/{agentId}/{sessionId}/events/..., which matches none of the routes, so CompositeFilesystem sends it to the default backend. A second mismatch sits behind it: the route is keyed on the builder's agentId, while the transcript middleware is registered on the inner ReActAgent and keys the transcript on the display name.

    The PR does what #3386 ended with: a ref of agents/{agentId}/sessions/{sessionId} for that store, built from resolvedAgentId, which HarnessAgent hands to TranscriptMiddleware and on to the writer. An end-to-end test builds an agent whose agentId differs from its display name and checks that the segments reach the store under agents/<agentId>/... and that nothing is left under the display name or on the local disk.

    Segments written before the change are not migrated. They only ever existed on the local disk of the node that wrote them, where the local context file remains the primary copy.

  10. aiyili commented on Dec 2, 2026

    @aiyili
    Contributor

    Merged into 2.0.x as ad8e2146 (#4450, including the review-scoped follow-up: only the builder's default store is keyed on the sessions route; a store supplied through transcriptStore(...) keeps its prefix and tenant layout).

    On main this code no longer exists — #3441 removed it — so there is nothing left to fix there. Closing this as completed on the maintenance branch seems right; maintainer's call.

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