xai-org/grok-build · error

Failed to fork session into worktree: {e}

Error message

Failed to fork session into worktree: {e}

What it means

resume_local_session_in_worktree forks a local session by calling fork_session; on failure it cleans up the just-created worktree and wraps the underlying error in this message. This is a context wrapper: the root cause is whatever fork_session returned (auth failure, registry error, session not found, etc.).

Source

Thrown at crates/codegen/xai-grok-shell/src/session/worktree.rs:435

    let worktree_root = std::path::Path::new(&wt_resp.worktree_path);
    let source_path = std::path::Path::new(resolved_source_cwd);
    let source_git_root = wt_resp.source_git_root.as_deref().map(std::path::Path::new);
    let effective_cwd = effective_worktree_path(worktree_root, source_path, source_git_root)
        .to_string_lossy()
        .to_string();
    let fork_req = ForkSessionRequest {
        source_session_id: resolved_session_id.to_owned(),
        source_cwd: resolved_source_cwd.to_owned(),
        new_cwd: effective_cwd.clone(),
        session_kind: Some("worktree".to_string()),
        source_workspace_dir: Some(resolved_source_cwd.to_owned()),
        ..Default::default()
    };
    let fork_resp = match fork_session(fork_req, agent_id, auth_manager).await {
        Ok(r) => r,
        Err(e) => {
            cleanup_worktree_on_failure(resolved_source_cwd, &wt_resp.worktree_path).await;
            return Err(anyhow::anyhow!("Failed to fork session into worktree: {e}"));
        }
    };
    Ok(ResumeSessionInWorktreeResponse {
        session_id: fork_resp.new_session_id,
        worktree_path: wt_resp.worktree_path,
        effective_cwd,
        remote_restored: false,
        parent_session_id: resolved_session_id.to_owned(),
        chat_messages_copied: fork_resp.chat_messages_copied,
        updates_copied: fork_resp.updates_copied,
        code_restored,
        restore_summary,
        restore_degree,
    })
}
/// Orchestrate session rehydration: recreate the git worktree at the exact
/// path and restore all session state using the original session ID.
///

View on GitHub (pinned to bc7f02eddd)

Solutions

  1. Read the wrapped source error in the message body ({e}) and fix that underlying cause first.
  2. Verify agent_id and auth_manager credentials are valid and non-expired before forking.
  3. Confirm the source session id exists in the registry/local store.
  4. Retry after network/auth issues; the worktree is cleaned up so no stale worktree remains.

Example fix

// before — blindly calls fork_session
let fork_resp = match fork_session(fork_req, agent_id, auth_manager).await {
    Ok(r) => r,
    Err(e) => { cleanup_worktree_on_failure(...).await; return Err(anyhow!("Failed to fork session into worktree: {e}")); }
};
// after — pre-validate before creating the worktree
ensure_session_forkable(&req.session_id, agent_id, auth_manager).await?; // fail before worktree creation
let fork_resp = fork_session(fork_req, agent_id, auth_manager).await?;
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-validate before creating the worktree
let auth_ok = auth_manager.validate().await.is_ok();
let exists = registry.get_session(&req.session_id).await.is_ok();
if !auth_ok || !exists { return Err(anyhow!("cannot fork: auth or session missing")); }

Try / catch

match resume_local_session_in_worktree(req).await {
    Err(e) if e.to_string().starts_with("Failed to fork session into worktree:") => {
        // worktree already cleaned up by the callee; inspect source cause
        log::error!("fork failed: {:#}", e);
        retry_with_fresh_auth(req).await
    }
    other => other,
}

Prevention

When it happens

Trigger: Calling resume_local_session_in_worktree when fork_session(fork_req, agent_id, auth_manager) returns Err — e.g. invalid session id, missing/invalid auth, registry unavailable.

Common situations: Resuming a local session whose registry record is gone or whose credentials expired; fork API rejected the request (validation error, quota, agent id mismatch).

Related errors


AI-assisted analysis of xai-org/grok-build@bc7f02eddd (2026-08-31). Data as JSON: /api/errors/49c9b3938efe106f. Report an issue: GitHub.