paperclipai/paperclip · error · Error

Target company ${companyId} was not found.

Error message

Target company ${companyId} was not found.

What it means

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.

Source

Thrown at cli/src/commands/worktree.ts:2672

      .select()
      .from(agents)
      .where(eq(agents.companyId, companyId)),
    input.targetDb
      .select()
      .from(projectWorkspaces)
      .where(eq(projectWorkspaces.companyId, companyId)),
    input.targetDb
      .select()
      .from(goals)
      .where(eq(goals.companyId, companyId)),
    input.sourceDb
      .select({ count: sql<number>`count(*)::int` })
      .from(heartbeatRuns)
      .where(eq(heartbeatRuns.companyId, companyId)),
  ]);

  if (!targetCompanyRow) {
    throw new Error(`Target company ${companyId} was not found.`);
  }

  const plan = buildWorktreeMergePlan({
    companyId,
    companyName: input.company.name,
    issuePrefix: input.company.issuePrefix,
    previewIssueCounterStart: targetCompanyRow.issueCounter,
    scopes: input.scopes,
    sourceIssues: sourceIssuesRows,
    targetIssues: targetIssuesRows,
    sourceComments: sourceCommentsRows,
    targetComments: targetCommentsRows,
    sourceProjects: sourceProjectsRows,
    sourceProjectWorkspaces: sourceProjectWorkspaceRows,
    sourceDocuments: sourceIssueDocumentsRows as IssueDocumentRow[],
    targetDocuments: targetIssueDocumentsRows as IssueDocumentRow[],
    sourceDocumentRevisions: sourceDocumentRevisionRows as DocumentRevisionRow[],
    targetDocumentRevisions: targetDocumentRevisionRows as DocumentRevisionRow[],

View on GitHub (pinned to 67001ec6eb)

Solutions

  1. Ensure no concurrent merges or resets run against the target while merge-history executes.
  2. Re-seed or recreate the company in the target, then re-run merge-history.
  3. Verify the target endpoint still points at the intended worktree DB.

Example fix

// before: target company deleted mid-merge
// after (restore then serialize)
paperclipai worktree:seed --from-config /source/.paperclip/config.json
# ensure no other merge/reset is running
paperclipai worktree:merge-history --company <id>
Defensive patterns

Strategy: try-catch

Validate before calling

if (!targetCompanyRow) throw new Error(`Target company ${companyId} missing; re-seed or recreate.`);

Type guard

function targetCompanyPresent(row: unknown): row is { issueCounter: number } {
  return row != null && typeof (row as any)?.issueCounter === "number";
}

Try / catch

try { await collectMergePlan(input); }
catch (err) {
  if (/Target company .* was not found/.test(String((err as Error).message))) {
    await reseedTarget(input.targetEndpoint); await collectMergePlan(input);
  } else throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of paperclipai/paperclip@67001ec6eb (2026-08-12). Data as JSON: /api/errors/b5f7ace0b2b09b6c. Report an issue: GitHub.