{"record":{"id":"b5f7ace0b2b09b6c","repo":"paperclipai/paperclip","slug":"target-company-companyid-was-not-found","errorCode":null,"errorMessage":"Target company ${companyId} was not found.","messagePattern":"Target company (.+?) was not found\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"cli/src/commands/worktree.ts","lineNumber":2672,"sourceCode":"      .select()\n      .from(agents)\n      .where(eq(agents.companyId, companyId)),\n    input.targetDb\n      .select()\n      .from(projectWorkspaces)\n      .where(eq(projectWorkspaces.companyId, companyId)),\n    input.targetDb\n      .select()\n      .from(goals)\n      .where(eq(goals.companyId, companyId)),\n    input.sourceDb\n      .select({ count: sql<number>`count(*)::int` })\n      .from(heartbeatRuns)\n      .where(eq(heartbeatRuns.companyId, companyId)),\n  ]);\n\n  if (!targetCompanyRow) {\n    throw new Error(`Target company ${companyId} was not found.`);\n  }\n\n  const plan = buildWorktreeMergePlan({\n    companyId,\n    companyName: input.company.name,\n    issuePrefix: input.company.issuePrefix,\n    previewIssueCounterStart: targetCompanyRow.issueCounter,\n    scopes: input.scopes,\n    sourceIssues: sourceIssuesRows,\n    targetIssues: targetIssuesRows,\n    sourceComments: sourceCommentsRows,\n    targetComments: targetCommentsRows,\n    sourceProjects: sourceProjectsRows,\n    sourceProjectWorkspaces: sourceProjectWorkspaceRows,\n    sourceDocuments: sourceIssueDocumentsRows as IssueDocumentRow[],\n    targetDocuments: targetIssueDocumentsRows as IssueDocumentRow[],\n    sourceDocumentRevisions: sourceDocumentRevisionRows as DocumentRevisionRow[],\n    targetDocumentRevisions: targetDocumentRevisionRows as DocumentRevisionRow[],","sourceCodeStart":2654,"sourceCodeEnd":2690,"githubUrl":"https://github.com/paperclipai/paperclip/blob/67001ec6eb96ae601aa27bc91d9b2415d665334a/cli/src/commands/worktree.ts#L2654-L2690","documentation":"Thrown inside collectMergePlan after querying the target companies table: targetCompanyRow is null for the resolved companyId. resolveMergeCompany already confirmed the company is shared, so a null target row here indicates the company vanished from the target between resolution and plan collection — a race, a concurrent delete, or an inconsistent read. The target company row is required because its issueCounter seeds previewIssueCounterStart.","triggerScenarios":"The company was deleted from the target DB after company resolution but before plan collection; a concurrent merge/seed mutated the target; reading from a target that was swapped mid-operation.","commonSituations":"Two merge processes running concurrently against the same target; target DB reset/wiped during the merge; company removed via API/CLI between the two queries.","solutions":["Ensure no concurrent merges or resets run against the target while merge-history executes.","Re-seed or recreate the company in the target, then re-run merge-history.","Verify the target endpoint still points at the intended worktree DB."],"exampleFix":"// before: target company deleted mid-merge\n// after (restore then serialize)\npaperclipai worktree:seed --from-config /source/.paperclip/config.json\n# ensure no other merge/reset is running\npaperclipai worktree:merge-history --company <id>","handlingStrategy":"try-catch","validationCode":"if (!targetCompanyRow) throw new Error(`Target company ${companyId} missing; re-seed or recreate.`);","typeGuard":"function targetCompanyPresent(row: unknown): row is { issueCounter: number } {\n  return row != null && typeof (row as any)?.issueCounter === \"number\";\n}","tryCatchPattern":"try { await collectMergePlan(input); }\ncatch (err) {\n  if (/Target company .* was not found/.test(String((err as Error).message))) {\n    await reseedTarget(input.targetEndpoint); await collectMergePlan(input);\n  } else throw err;\n}","preventionTips":["Serialize merge-history runs; do not delete/reset the target mid-merge.","Re-seed the target before merging if companies are missing.","Lock the target endpoint for the merge duration."],"tags":["worktree","merge","company","concurrency"],"backgroundTag":null,"analyzedSha":"67001ec6eb96ae601aa27bc91d9b2415d665334a","analyzedAt":"2026-08-12T12:05:45.408Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}