Grok Build · Context Isolation

子 Agent 的四個隔離維度

上下文來源、身份連續性、工作目錄和檔案改動空間分別控制不同邊界。把它們混成一個「隔離等級」,容易誤讀恢復和 worktree 的真實行為。

課程目標

能準確解釋 ContextSource::New / ResumedResumeSourceDataSubagentIsolationMode,並根據任務選擇新會話、恢復與 worktree。

TEACHING DIAGRAM

隔離是一組正交維度

同一個 Resumed 子 Agent 可以複用 worktree,也可以繼承普通 cwd。上下文連續與檔案空間隔離要分別判斷。

上下文、身份、工作目錄和改動空間四個隔離維度 childsession ContextSourceNew / Resumed Identitytype / persona / model pin Effective cwdworktree > override > parent Isolation modeNone / 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.rsREAL 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 負責定位身份、目錄、會話和快照。先拆維度,再談隔離強度。