836 字
约 2 分钟
1
子 Agent 的四个隔离维度
无标签

Grok Build · Context Isolation 子 Agent 的四个隔离维度 上下文来源、身份连续性、工作目录和文件改动空间分别控制不同边界。把它们混成一个「隔离等级」,容易误读恢复和 worktree 的真实行为。 一句话速览 从上下文来源、恢复模式、工作树和任务状态分析隔离边界 课程目标 能准确解释 ContextSource::New / Resumed 、 ResumeSourceData 、 SubagentIsolationMode ,并根据任务选择新会话、恢复与 worktree。

TEACHING DIAGRAM 隔离是一组正交维度 同一个 Resumed 子 Agent 可以复用 worktree,也可以继承普通 cwd。上下文连续与文件空间隔离要分别判断。

上下文、身份、工作目录和改动空间四个隔离维度

child session ContextSource New / Resumed Identity type / persona / model pin Effective cwd worktree > override > parent Isolation mode None / Worktree

ContextSource 的公开语义

ContextSource::New 新会话不继承历史。spawn 流程建立新的 system prompt 和 prompt context,再接收当前任务输入。它不代表独立文件空间,文件空间仍取决于 isolation 与 cwd。

ContextSource::Resumed 从已完成的 peer subagent 继续。源码复制原始 transcript 与 tool state,模型沿用 source model;system prompt 和 prompt context 根据当前 AgentDefinition 重新渲染。

实现补充: xai-grok-subagent-resolution::ContextSource 公开枚举只有 New 与 Resumed。shell 内部还有 InitialContextSource::Forked ,用于从父会话镜像或摘要上下文;它属于另一条 bootstrap 分支,不应伪装成公开枚举成员。

ResumeSourceData 传递恢复定位信息

身份 subagent_id、type、persona、model_id 目录 child_cwd、worktree_path 会话数据 child_session_id 定位 transcript 与 tool state 快照 snapshot_ref 可重建已删除 worktree

身份校验 subagent_type 必须与 source 一致。 显式 Persona 必须与 source 一致;未显式给出则沿用 source。 model 不参与身份 gate。请求中的 model override 会被软忽略,并 pin 到 source model。 恢复限制 复制 transcript 或读取失败时,显式 resume 会失败关闭。 source transcript 超过目标模型上下文窗口 80% 时拒绝恢复。 恢复复制 tool state,不复制 plan state、plan mode state 与 signals。

SubagentIsolationMode 只有两种

None 子 Agent 使用解析后的 cwd,通常与父工作区相同。上下文窗口依然是独立子会话。None 描述文件工作空间隔离,不等于共享对话历史。 Worktree 子 Agent 使用独立 worktree 路径。恢复时优先复用 source worktree;路径已移除且存在 snapshot_ref 时,可从持久 git ref 重新水化。该枚举没有「sandbox」成员。

crates/codegen/xai-grok-subagent-resolution/src/types.rs crates/codegen/xai-grok-subagent-resolution/src/resume.rs crates/common/xai-tool-types/src/task.rs bootstrap_initial_context resolve_child_cwd

真实源码快照

crates/common/xai-tool-types/src/task.rs REAL SOURCE

pub enum SubagentIsolationMode {
 #[default]
 #[serde(alias = "None")]
 None,
 #[serde(alias = "Worktree", alias = "work_tree", alias = "work-tree")]
 Worktree,
}

快照说明: 枚举完整保留,展示 wire alias。上方同心圆是课程视觉,用于强调维度正交,并非源码中的对象层级。

课堂练习:给两个任务选边界 任务 A 要继续昨天完成一半的重构,并保留原 worktree 未提交改动。任务 B 只需独立分析当前仓库。分别选择 New 或 Resumed、None 或 Worktree,并说明 transcript、model、cwd 与文件改动空间如何处理。

Takeaway: New 与 Resumed 控制信息连续性,None 与 Worktree 控制文件空间,ResumeSourceData 负责定位身份、目录、会话和快照。先拆维度,再谈隔离强度。

子 Agent 的四个隔离维度
http://www.clxhxhhr.top/posts/4004/
作者
clxstart
发布于
2026-09-25
许可协议
CC BY-NC-SA 4.0
评论
0 条
还没有评论,先写一条吧。