{"record":{"id":"9a92cf89cf5cd20f","repo":"xai-org/grok-build","slug":"worktree-not-found-id","errorCode":null,"errorMessage":"worktree not found: {id}","messagePattern":"worktree not found: (.+?)","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-workspace/src/worktree/mod.rs","lineNumber":1334,"sourceCode":"    }\n}\n\n// ============================================================================\n// Remove Worktree\n// ============================================================================\n\npub async fn remove_worktree(\n    req: &RemoveWorktreeRequest,\n    copy_context: &BackgroundCopyContext,\n) -> Result<RemoveWorktreeResponse> {\n    let resolved = match (&req.worktree_path, &req.id_or_path) {\n        (Some(_), Some(_)) => {\n            anyhow::bail!(\"exactly one of worktreePath or idOrPath must be set, not both\")\n        }\n        (Some(path), None) => path.clone(),\n        (None, Some(id)) => match resolve_worktree_by_id_or_path(id)? {\n            Some(p) => p.display().to_string(),\n            None => anyhow::bail!(\"worktree not found: {id}\"),\n        },\n        (None, None) => anyhow::bail!(\"either worktreePath or idOrPath must be set\"),\n    };\n    let worktree_path = Path::new(&resolved);\n\n    tracing::info!(\n        target: WORKTREE_LOG,\n        path = %resolved,\n        force = req.force,\n        dry_run = req.dry_run,\n        \"REMOVE_START: removing worktree\"\n    );\n\n    // jj workspace: detect by .jj/repo and route to jj-specific cleanup.\n    if worktree_path.join(\".jj\").join(\"repo\").exists() {\n        if req.dry_run {\n            return Ok(RemoveWorktreeResponse {\n                removed: false,","sourceCodeStart":1316,"sourceCodeEnd":1352,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-workspace/src/worktree/mod.rs#L1316-L1352","documentation":"When remove_worktree receives only id_or_path, it calls resolve_worktree_by_id_or_path to map the identifier to a real worktree path. If resolution returns None — the id is unknown, stale, or the path does not correspond to a registered worktree — this error is thrown instead of proceeding.","triggerScenarios":"Calling remove_worktree with id_or_path set to: a worktree id that was already removed, an id that was never created in this workspace, or a path string that no longer matches any registered worktree entry.","commonSituations":"Stale references after a worktree was removed in another session; typos in ids; passing a repo path that is the main checkout, not a registered worktree; workspace metadata reset or migrated between runs.","solutions":["List existing worktrees and confirm the exact id_or_path before removing","Remove the worktree by its resolved absolute worktree_path instead of id_or_path","Recreate the worktree (or re-register it) if it was deleted elsewhere and you still need it","Guard the call: resolve_worktree_by_id_or_path first and skip removal when it returns None"],"exampleFix":"// before\nremove_worktree(&RemoveWorktreeRequest { id_or_path: Some(\"wt-42\".into()), ..Default::default() }, &ctx).await?;\n// after\nif resolve_worktree_by_id_or_path(\"wt-42\")?.is_some() {\n    remove_worktree(&RemoveWorktreeRequest { id_or_path: Some(\"wt-42\".into()), ..Default::default() }, &ctx).await?;\n}","handlingStrategy":"validation","validationCode":"let resolved = resolve_worktree_by_id_or_path(id)?;\nanyhow::ensure!(resolved.is_some(), \"worktree {id} does not exist; list worktrees first\");","typeGuard":"fn worktree_exists(id: &str) -> bool {\n    resolve_worktree_by_id_or_path(id).map(|r| r.is_some()).unwrap_or(false)\n}","tryCatchPattern":"match remove_worktree(&req, &ctx).await {\n    Err(e) if e.to_string().starts_with(\"worktree not found\") => {\n        tracing::warn!(\"worktree already gone, skipping\");\n    }\n    other => other?,\n}","preventionTips":["Resolve ids from a fresh `list worktrees` call rather than caching them across runs","Skip removal gracefully when the target no longer exists (idempotent cleanup)","Prefer absolute worktree_path for scripts that already know the directory"],"tags":["not-found","worktree","rust"],"backgroundTag":"resource-not-found","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}