{"record":{"id":"527a53c7ddc29c63","repo":"paperclipai/paperclip","slug":"target-worktree-database-appears-to-be-running-pi-527a53","errorCode":null,"errorMessage":"Target worktree database appears to be running (pid ${runningTargetPid}). Stop Paperclip in ${target.rootPath} before repairing, or re-run with --allow-live-target if you want to override this guard.","messagePattern":"Target worktree database appears to be running \\(pid (.+?)\\)\\. Stop Paperclip in (.+?) before repairing, or re-run with --allow-live-target if you want to override this guard\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"cli/src/commands/worktree.ts","lineNumber":3554,"sourceCode":"      fromConfig: source.configPath,\n      to: target.rootPath,\n      seedMode,\n      preserveLiveWork: opts.preserveLiveWork,\n      yes: true,\n      allowLiveTarget: opts.allowLiveTarget,\n    });\n    return;\n  }\n\n  const repairInstanceId = sanitizeWorktreeInstanceId(path.basename(target.rootPath));\n  const repairPaths = resolveWorktreeLocalPaths({\n    cwd: target.rootPath,\n    homeDir: resolveWorktreeHome(opts.home),\n    instanceId: repairInstanceId,\n  });\n  const runningTargetPid = readRunningPostmasterPid(path.resolve(repairPaths.embeddedPostgresDataDir, \"postmaster.pid\"));\n  if (runningTargetPid && !opts.allowLiveTarget) {\n    throw new Error(\n      `Target worktree database appears to be running (pid ${runningTargetPid}). Stop Paperclip in ${target.rootPath} before repairing, or re-run with --allow-live-target if you want to override this guard.`,\n    );\n  }\n  if (runningTargetPid && opts.allowLiveTarget) {\n    p.log.warning(`Proceeding even though the target embedded PostgreSQL appears to be running (pid ${runningTargetPid}).`);\n  }\n\n  const originalCwd = process.cwd();\n  try {\n    process.chdir(target.rootPath);\n    await runWorktreeInit({\n      home: opts.home,\n      fromConfig: source.configPath,\n      fromDataDir: opts.fromDataDir,\n      fromInstance: opts.fromInstance,\n      seed: opts.noSeed ? false : true,\n      seedMode,\n      preserveLiveWork: opts.preserveLiveWork,","sourceCodeStart":3536,"sourceCodeEnd":3572,"githubUrl":"https://github.com/paperclipai/paperclip/blob/67001ec6eb96ae601aa27bc91d9b2415d665334a/cli/src/commands/worktree.ts#L3536-L3572","documentation":"Thrown in the cold-init branch of worktreeRepairCommand (the path taken when the target has no worktree-local config/env yet) when readRunningPostmasterPid finds a postmaster.pid in the target's embedded-postgres data dir whose pid is alive (process.kill(pid,0) succeeds) and --allow-live-target is not set. This protects against repairing/overwriting a DB that is actively running. A stale pid file (dead process) does NOT trip this guard because readRunningPostmasterPid returns null in that case.","triggerScenarios":"Calling `paperclipai worktree repair --branch <name>` where the target worktree's embedded PostgreSQL data dir contains a postmaster.pid whose process is still alive, without --allow-live-target.","commonSituations":"A previous `paperclipai worktree init`/`dev` left embedded PostgreSQL running in the target worktree. Or a crashed Paperclip process left embedded PostgreSQL orphaned but still running. The repair command wants to (re)initialize that data dir, so it refuses to clobber a live DB.","solutions":["Stop the embedded PostgreSQL / Paperclip server holding that pid (e.g. stop `paperclipai dev` in the target worktree), then retry.","Re-run with --allow-live-target to override the guard if you are sure overwriting is safe.","Confirm the pid is genuinely Paperclip's embedded PG with `ps -p <pid> -o command=` before killing anything."],"exampleFix":"// before\npaperclipai worktree repair --branch feature-x\n// after (stop the live target DB first)\n# stop paperclipai dev / server in the target worktree, then\npaperclipai worktree repair --branch feature-x\n// or override\npaperclipai worktree repair --branch feature-x --allow-live-target","handlingStrategy":"validation","validationCode":"import fs from \"node:fs\";\nimport path from \"node:path\";\n\nfunction readLivePostmasterPid(dataDir: string): number | null {\n  const pidFile = path.resolve(dataDir, \"postmaster.pid\");\n  if (!fs.existsSync(pidFile)) return null;\n  const pid = Number(fs.readFileSync(pidFile, \"utf8\").split(\"\\n\")[0]?.trim());\n  if (!Number.isInteger(pid) || pid <= 0) return null;\n  try { process.kill(pid, 0); return pid; } catch { return null; }\n}\n\n// before repair (cold branch), compute repairPaths.embeddedPostgresDataDir and check:\nconst livePid = readLivePostmasterPid(repairPaths.embeddedPostgresDataDir);\nif (livePid && !opts.allowLiveTarget) {\n  throw new Error(`Stop embedded PostgreSQL (pid ${livePid}) in the target worktree before repair, or pass --allow-live-target.`);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure no `paperclipai dev`/server is running in the target worktree before invoking repair.","Before repair, check the target's embedded-postgres data dir for a live postmaster.pid using process.kill(pid,0) semantics.","Remember a stale (dead-pid) postmaster.pid does NOT trip this guard; only live processes do."],"tags":["worktree","cli","repair","postgres","safety-guard"],"backgroundTag":null,"analyzedSha":"67001ec6eb96ae601aa27bc91d9b2415d665334a","analyzedAt":"2026-08-12T12:05:45.408Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}