{"record":{"id":"6b63f1d2e924b961","repo":"paperclipai/paperclip","slug":"failed-to-merge-concurrent-remote-git-histories-fo","errorCode":null,"errorMessage":"Failed to merge concurrent remote git histories for ${currentHead.slice(0, 12)} and ${input.importedHead.slice(0, 12)}: ${reason}","messagePattern":"Failed to merge concurrent remote git histories for (.+?) and (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/adapter-utils/src/git-workspace-sync.ts","lineNumber":426,"sourceCode":"          timeout: 10_000,\n          maxBuffer: 16 * 1024,\n        });\n        return;\n      } catch (error) {\n        if (isConcurrentRefUpdateError(error) && attempt < 4) continue;\n        throw error;\n      }\n    }\n\n    let mergedTree;\n    try {\n      mergedTree = await runLocalGit(input.localDir, [\"merge-tree\", \"--write-tree\", currentHead, input.importedHead], {\n        timeout: 60_000,\n        maxBuffer: 256 * 1024,\n      });\n    } catch (error) {\n      const reason = error instanceof Error ? error.message : String(error);\n      throw new Error(\n        `Failed to merge concurrent remote git histories for ${currentHead.slice(0, 12)} and ${input.importedHead.slice(0, 12)}: ${reason}`,\n      );\n    }\n    const mergedTreeId = mergedTree.stdout.trim().split(\"\\n\")[0]?.trim() ?? \"\";\n    if (!mergedTreeId) {\n      throw new Error(\"Failed to compute a merged git tree for workspace restore.\");\n    }\n\n    const mergeCommit = await runLocalGit(\n      input.localDir,\n      [\n        \"commit-tree\",\n        mergedTreeId,\n        \"-p\",\n        currentHead,\n        \"-p\",\n        input.importedHead,\n        \"-m\",","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/paperclipai/paperclip/blob/67001ec6eb96ae601aa27bc91d9b2415d665334a/packages/adapter-utils/src/git-workspace-sync.ts#L408-L444","documentation":"Thrown by integrateImportedGitHead when `git merge-tree --write-tree <currentHead> <importedHead>` fails with an error. This happens during workspace sync when the local head and the imported remote head have divergent histories that git cannot auto-merge (true merge conflicts) or when git itself errors (corrupt repo, invalid refs).","triggerScenarios":"Calling integrateImportedGitHead({ localDir, importedHead }) where the merge-base check shows the two heads have diverged (neither is an ancestor of the other). The subsequent `git merge-tree --write-tree` invocation throws, and the error message is captured into the reason.","commonSituations":"Two concurrent agent runs modified overlapping files causing a content conflict; the local git repo is corrupt or has a detached HEAD in an unexpected state; the importedHead SHA is invalid or points to a different repo; git version is too old to support `merge-tree --write-tree` (requires git 2.38+).","solutions":["Check the reason string in the error message for git's conflict details and resolve the conflicting files.","Ensure the local git repo is healthy (git fsck) and HEAD points to a valid commit.","Upgrade git to 2.38+ if `merge-tree --write-tree` is not supported.","If conflicts are due to concurrent agents, serialize workspace access or partition work to avoid overlapping file edits."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Pre-check for merge conflicts before integrating\nasync function canMergeWithoutConflict(localDir: string, currentHead: string, importedHead: string): Promise<boolean> {\n  try {\n    const result = await runLocalGit(localDir, [\"merge-tree\", \"--write-tree\", currentHead, importedHead], {\n      timeout: 60_000,\n      maxBuffer: 256 * 1024,\n    });\n    return result.stdout.trim().length > 0;\n  } catch {\n    return false;\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await integrateImportedGitHead({ localDir, importedHead });\n} catch (err) {\n  if (err instanceof Error && err.message.includes(\"Failed to merge concurrent remote git histories\")) {\n    // Fall back to a hard reset or manual conflict resolution\n    logger.error(`Git merge conflict during workspace sync: ${err.message}`);\n    throw err;\n  }\n  throw err;\n}","preventionTips":["Serialize workspace sync operations to avoid concurrent divergent histories.","Partition agent work to minimize overlapping file edits between concurrent runs.","Run `git fsck` periodically to maintain repo health.","Ensure git 2.38+ is installed for merge-tree --write-tree support."],"tags":["git","workspace-sync","merge-conflict","adapter-utils"],"backgroundTag":null,"analyzedSha":"67001ec6eb96ae601aa27bc91d9b2415d665334a","analyzedAt":"2026-08-12T12:05:45.408Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}