Skip to content

[harness] cloneLocalSpecForSubagent 丢失 isolationScope 与 spec 子类,声明式 ISOLATED 子 Agent 回落到 USER 作用域 #4023

Open

Description

@chenrongquan

版本

项目 版本
使用版本 io.agentscope:agentscope-harness:2.0.4(Maven Central,Latest release v2.0.4),同时依赖 agentscope-core / agentscope-extensions 2.0.4
同样存在 v2.1.0-BETA1(Pre-release)、main 分支(2.1.0-SNAPSHOT,提交 7ad4939f4,2026-11-06 核对)
运行环境 JDK 21,LocalFilesystemSpec(未启用沙箱)

官方文档的描述

ISOLATED(默认):子 agent 有自己独立的工作区(如果声明里 workspace.path 没写,框架会自动开一个子目录)。子 agent 的运行时状态按"父 sessionId × 用户"分桶——同一用户在不同对话里 spawn 同名子 agent 也互不污染。
—— https://java.agentscope.io/v2/zh/docs/harness/subagent (「ISOLATED vs SHARED」)

userId 透传:父的 RuntimeContext.userId 会自动透到子,所以多租户隔离链不会断。
—— 同页「一些行为细节」

实际行为

HarnessAgentBuilderSupport.cloneLocalSpecForSubagent:

LocalFilesystemSpec spec = new LocalFilesystemSpec();
if (parent.getProject() != null) spec.project(parent.getProject());
if (parent.getMode() != null)    spec.mode(parent.getMode());
spec.additionalRoots(parent.getAdditionalRoots());
return spec;

没有复制的字段:isolationScope、sharedLocalWorkspace、projectWritable、执行超时与输出上限、env / inheritEnv,以及具体子类(始终 new LocalFilesystemSpec())。

由于克隆出的 isolationScope 为 null,HarnessAgent.Builder.build() 回落到硬编码默认值:

IsolationScope fsIsolationScope = IsolationScope.USER;

于是父 Agent 配置了 isolationScope(AGENT)(不加命名空间前缀),子 Agent 却会给每个路径加上 RuntimeContext.userId 前缀。影响:

  1. 子 Agent 的运行数据(记忆、会话、任务)写到 <子工作区>/<userId>/...,这是父 Agent 从未配置过的目录布局。
  2. 父 Agent 上配置的限制(执行超时、最大输出字节数,或覆写 toFilesystem 的子类)对子 Agent 不生效。

期望

克隆应保留父 spec 中所有承载策略的字段——至少要包括 isolationScope——最好也保留子类(例如在 LocalFilesystemSpec 上提供可被子类覆写的 copy() 方法),使声明式 ISOLATED 子 Agent 与父 Agent 的区别只有工作区根目录。

相关

HarnessAgent.Builder.build() 中 workspaceFor(uid, sid) 工厂里硬编码的 ctxNs = uid -> List.of(uid) 同样忽略了已配置的 isolationScope,建议在同一次修改中改为与 fsIsolationScope.toNamespaceFactory() 一致。

Activity

  1. wcy12378 commented on Nov 6, 2026

    @wcy12378

    I'd like to work on this bug. I plan to preserve the configured LocalFilesystemSpec when materializing a declarative ISOLATED subagent, keep its workspace separate, and use the configured namespace factory in the context-bound workspace path. I'll add regressions for inherited policy, custom spec behavior, and user/session isolation before preparing a focused PR.

    This contribution will be developed with AI assistance under the account owner's authorization.

  2. wahllllll commented on Nov 7, 2026

    @wahllllll
    Contributor

    Confirmed on main, in HarnessAgentBuilderSupport.cloneLocalSpecForSubagent: it copies project, mode and additionalRoots, then builds a fresh LocalFilesystemSpec. So isolationScope, sharedLocalWorkspace, projectWritable, executeTimeoutSeconds, maxOutputBytes, env and inheritEnv all fall back to their defaults, and any overriding subclass is discarded in favour of the base type.

    The consequence you describe follows directly: build() falls back to IsolationScope.USER, every path gains a userId prefix the parent never configured, and the parent's toFilesystem override stops applying to its own subagents.

    What I would change:

    • add copy() to LocalFilesystemSpec that carries every field and keeps the concrete type (a protected newInstance() or a virtual copy()), so a cloned spec differs from its parent only in the workspace root;
    • have cloneLocalSpecForSubagent call it rather than hand-picking three fields — that way the next field added to the spec cannot be silently dropped again;
    • align the hardcoded ctxNs = uid -> List.of(uid) in workspaceFor with fsIsolationScope.toNamespaceFactory(), so the state/namespace layout matches the isolation scope that is actually configured.

    Test: a parent with isolationScope(AGENT) plus non-default timeout and env → the spawned subagent exposes the same values, and the namespace factory yields no user prefix.

    Two questions before I start, both about scope rather than about the bug:

    1. You mention the RemoteFilesystemSpec#sharedLocalWorkspace asymmetry ([Bug]:read_file/list_files 无法读取应用服务写入工作区根目录的文件:default backend 命名空间前缀导致读写不对称 #3245) — should copy() be defined symmetrically across both specs in the same change, or is the local one enough for now?
    2. Do you need the subclass itself preserved, or would copying the fields by name on the base type cover your case? Preserving the type is stricter and I would rather do it, but it is the part with more room for surprises.

    I would like to take this one unless you are already on it.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions