{"record":{"id":"8884b45a4b50a556","repo":"can1357/oh-my-pi","slug":"git-apply-3way-failed-for-task-taskid-stde","errorCode":null,"errorMessage":"git apply --3way failed for task ${taskId}: ${stderr}","messagePattern":"git apply --3way failed for task (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/task/worktree.ts","lineNumber":660,"sourceCode":"\t\ttry {\n\t\t\tawait repo.applyPatch(patchText, { threeWay: true });\n\t\t} catch (err) {\n\t\t\tif (!vcs.isVcsError(err)) throw err;\n\t\t\tthreeWayErr = err;\n\t\t}\n\t\tif (threeWayErr) {\n\t\t\tconst wipPatches = collectWipPatches(baselineWip);\n\t\t\tif (wipPatches.length === 0 || !baselineWip) {\n\t\t\t\tconst stderr = threeWayErr.stderr.slice(0, 2000);\n\t\t\t\tlogger.error(\"commitToBranch: git apply --3way failed\", {\n\t\t\t\t\ttaskId,\n\t\t\t\t\texitCode: threeWayErr.exitCode,\n\t\t\t\t\tstderr,\n\t\t\t\t\tinitialStderr: plainErr.stderr.slice(0, 2000),\n\t\t\t\t\tpatchSize: patchText.length,\n\t\t\t\t\tpatchHead: patchText.slice(0, 500),\n\t\t\t\t});\n\t\t\t\tthrow new Error(`git apply --3way failed for task ${taskId}: ${stderr}`);\n\t\t\t}\n\t\t\ttry {\n\t\t\t\t// `git apply --3way` leaves conflict markers in `U` files when\n\t\t\t\t// it can't resolve; reset the worktree so the WIP-seeded retry\n\t\t\t\t// starts from a clean HEAD tree.\n\t\t\t\tawait repo.reset(\"hard\", \"HEAD\");\n\t\t\t\tawait applyDeltaOverBaselineWip(tmpDir, taskId, patchText, wipPatches, baselineWip);\n\t\t\t} catch (wipErr) {\n\t\t\t\tif (!vcs.isVcsError(wipErr)) throw wipErr;\n\t\t\t\tconst stderr = wipErr.stderr.slice(0, 2000);\n\t\t\t\tlogger.error(\"commitToBranch: git apply with baseline WIP failed\", {\n\t\t\t\t\ttaskId,\n\t\t\t\t\texitCode: wipErr.exitCode,\n\t\t\t\t\tstderr,\n\t\t\t\t\tthreeWayStderr: threeWayErr.stderr.slice(0, 2000),\n\t\t\t\t\tinitialStderr: plainErr.stderr.slice(0, 2000),\n\t\t\t\t\tpatchSize: patchText.length,\n\t\t\t\t\tpatchHead: patchText.slice(0, 500),","sourceCodeStart":642,"sourceCodeEnd":678,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/task/worktree.ts#L642-L678","documentation":"When merging an isolated task's patch back into the parent worktree, the code first tries plain `git apply`, then retries with `git apply --3way`. If the three-way attempt also fails, it logs diagnostics (exit code, stderr, patch head) and throws with the git stderr embedded so the conflict cause is visible.","triggerScenarios":"Calling the patch-application path for a task whose diff cannot be applied even with 3-way merge: files changed in both the task worktree and the parent (real conflicts), missing context, or paths deleted/renamed in the parent since the task started.","commonSituations":"Long-running subagent tasks whose target files were edited in the main worktree meanwhile; binary files (git apply --3way can't merge binaries); patches produced against a stale baseline commit.","solutions":["Read the embedded stderr to find the conflicting file(s); manually apply the task's changes or rebase them","Reduce conflict window: merge task results promptly, or re-run the task against the current parent HEAD","If binary files conflict, apply those hunks manually"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// dry-run the patch before the real 3-way apply\nconst probe = await $`git apply --check --3way patch.diff`.cwd(repoDir).quiet().nothrow();\nif (probe.exitCode !== 0) console.error(probe.stderr.text());","typeGuard":null,"tryCatchPattern":"try {\n\tawait mergeTaskPatch(taskId, patchText);\n} catch (err) {\n\tif ((err as Error).message.startsWith(\"git apply --3way failed\")) {\n\t\t// rebase task branch onto current HEAD and regenerate patch\n\t}\n\tthrow err;\n}","preventionTips":["Merge task results promptly before editing the same files in the parent","Avoid having tasks and the parent touch the same paths","Keep binary files out of isolated-task diffs (3-way merge can't merge binaries)"],"tags":["git","patch-apply","merge-conflict"],"backgroundTag":"git-apply-3way-failed","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}