{"record":{"id":"49c9b3938efe106f","repo":"xai-org/grok-build","slug":"failed-to-fork-session-into-worktree-e","errorCode":null,"errorMessage":"Failed to fork session into worktree: {e}","messagePattern":"Failed to fork session into worktree: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-shell/src/session/worktree.rs","lineNumber":435,"sourceCode":"    let worktree_root = std::path::Path::new(&wt_resp.worktree_path);\n    let source_path = std::path::Path::new(resolved_source_cwd);\n    let source_git_root = wt_resp.source_git_root.as_deref().map(std::path::Path::new);\n    let effective_cwd = effective_worktree_path(worktree_root, source_path, source_git_root)\n        .to_string_lossy()\n        .to_string();\n    let fork_req = ForkSessionRequest {\n        source_session_id: resolved_session_id.to_owned(),\n        source_cwd: resolved_source_cwd.to_owned(),\n        new_cwd: effective_cwd.clone(),\n        session_kind: Some(\"worktree\".to_string()),\n        source_workspace_dir: Some(resolved_source_cwd.to_owned()),\n        ..Default::default()\n    };\n    let fork_resp = match fork_session(fork_req, agent_id, auth_manager).await {\n        Ok(r) => r,\n        Err(e) => {\n            cleanup_worktree_on_failure(resolved_source_cwd, &wt_resp.worktree_path).await;\n            return Err(anyhow::anyhow!(\"Failed to fork session into worktree: {e}\"));\n        }\n    };\n    Ok(ResumeSessionInWorktreeResponse {\n        session_id: fork_resp.new_session_id,\n        worktree_path: wt_resp.worktree_path,\n        effective_cwd,\n        remote_restored: false,\n        parent_session_id: resolved_session_id.to_owned(),\n        chat_messages_copied: fork_resp.chat_messages_copied,\n        updates_copied: fork_resp.updates_copied,\n        code_restored,\n        restore_summary,\n        restore_degree,\n    })\n}\n/// Orchestrate session rehydration: recreate the git worktree at the exact\n/// path and restore all session state using the original session ID.\n///","sourceCodeStart":417,"sourceCodeEnd":453,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-shell/src/session/worktree.rs#L417-L453","documentation":"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.).","triggerScenarios":"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.","commonSituations":"Resuming a local session whose registry record is gone or whose credentials expired; fork API rejected the request (validation error, quota, agent id mismatch).","solutions":["Read the wrapped source error in the message body ({e}) and fix that underlying cause first.","Verify agent_id and auth_manager credentials are valid and non-expired before forking.","Confirm the source session id exists in the registry/local store.","Retry after network/auth issues; the worktree is cleaned up so no stale worktree remains."],"exampleFix":"// before — blindly calls fork_session\nlet fork_resp = match fork_session(fork_req, agent_id, auth_manager).await {\n    Ok(r) => r,\n    Err(e) => { cleanup_worktree_on_failure(...).await; return Err(anyhow!(\"Failed to fork session into worktree: {e}\")); }\n};\n// after — pre-validate before creating the worktree\nensure_session_forkable(&req.session_id, agent_id, auth_manager).await?; // fail before worktree creation\nlet fork_resp = fork_session(fork_req, agent_id, auth_manager).await?;","handlingStrategy":"try-catch","validationCode":"// Pre-validate before creating the worktree\nlet auth_ok = auth_manager.validate().await.is_ok();\nlet exists = registry.get_session(&req.session_id).await.is_ok();\nif !auth_ok || !exists { return Err(anyhow!(\"cannot fork: auth or session missing\")); }","typeGuard":null,"tryCatchPattern":"match resume_local_session_in_worktree(req).await {\n    Err(e) if e.to_string().starts_with(\"Failed to fork session into worktree:\") => {\n        // worktree already cleaned up by the callee; inspect source cause\n        log::error!(\"fork failed: {:#}\", e);\n        retry_with_fresh_auth(req).await\n    }\n    other => other,\n}","preventionTips":["Validate the source session id against the registry before forking","Refresh auth tokens before long-lived resume flows","Don't create the worktree until fork prerequisites are verified","Always log the full error chain, not just the wrapper message"],"tags":["rust","session-fork","worktree","error-wrapping"],"backgroundTag":"fork-session-failed","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}