{"record":{"id":"060b2994d49af636","repo":"paperclipai/paperclip","slug":"failed-to-ensure-local-environment","errorCode":null,"errorMessage":"Failed to ensure local environment","messagePattern":"Failed to ensure local environment","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/src/services/environments.ts","lineNumber":908,"sourceCode":"            where: sql`${environments.driver} = 'local'`,\n          })\n          .returning()\n          .then((rows) => rows[0] ?? null);\n      const row = await insert().catch((error: unknown) => {\n        if (hasConstraintName(error, \"environments_name_idx\")) {\n          return null;\n        }\n        throw error;\n      });\n      if (row) return toEnvironment(row);\n\n      const existing = await db\n        .select()\n        .from(environments)\n        .where(eq(environments.driver, \"local\"))\n        .then((rows) => rows[0] ?? null);\n      if (!existing) {\n        throw new Error(\"Failed to ensure local environment\");\n      }\n      const existingMetadata = (existing.metadata ?? {}) as Record<string, unknown>;\n      if (isCloudManagedInstance() && existingMetadata.managedByPaperclip !== true) {\n        const adopted = await db\n          .update(environments)\n          .set({\n            metadata: { ...existingMetadata, managedByPaperclip: true },\n            updatedAt: new Date(),\n          })\n          .where(eq(environments.id, existing.id))\n          .returning()\n          .then((rows) => rows[0] ?? existing);\n        return toEnvironment(adopted);\n      }\n      return toEnvironment(existing);\n    },\n\n    ensureManagedSandboxEnvironment,","sourceCodeStart":890,"sourceCodeEnd":926,"githubUrl":"https://github.com/paperclipai/paperclip/blob/01ad8584922b5d85292b1723cae71fa0d9b07a19/server/src/services/environments.ts#L890-L926","documentation":"Unexpected-outcome guard in ensureLocalEnvironment: after a unique-name insert race on environments_name_idx, the fallback select for an existing local-driver row also found nothing. The local environment should be guaranteed after this path, so its absence is an invariant violation and the service fails.","triggerScenarios":"Thrown at server/src/services/environments.ts:908 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check server logs for the failure ensuring the local environment (path, permissions), then retry."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"01ad8584922b5d85292b1723cae71fa0d9b07a19","analyzedAt":"2026-08-18T22:49:45.177Z","contentChangedAt":"2026-08-18T22:49:45.177Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}