@mastra/factory 0.0.0-fix-thread-subscription-multi-instance-redis-streams-20260730204150

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (313) hide show
  1. package/CHANGELOG.md +695 -0
  2. package/LICENSE.md +30 -0
  3. package/README.md +27 -0
  4. package/dist/auth.d.ts +181 -0
  5. package/dist/auth.d.ts.map +1 -0
  6. package/dist/auth.js +505 -0
  7. package/dist/auth.js.map +1 -0
  8. package/dist/capabilities/connection.d.ts +9 -0
  9. package/dist/capabilities/connection.d.ts.map +1 -0
  10. package/dist/capabilities/connection.js +1 -0
  11. package/dist/capabilities/intake.d.ts +140 -0
  12. package/dist/capabilities/intake.d.ts.map +1 -0
  13. package/dist/capabilities/intake.js +1 -0
  14. package/dist/capabilities/version-control.d.ts +245 -0
  15. package/dist/capabilities/version-control.d.ts.map +1 -0
  16. package/dist/capabilities/version-control.js +1 -0
  17. package/dist/factory.d.ts +156 -0
  18. package/dist/factory.d.ts.map +1 -0
  19. package/dist/factory.js +483 -0
  20. package/dist/factory.js.map +1 -0
  21. package/dist/index.d.ts +14 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +7 -0
  24. package/dist/integrations/base.d.ts +210 -0
  25. package/dist/integrations/base.d.ts.map +1 -0
  26. package/dist/integrations/base.js +1 -0
  27. package/dist/integrations/github/config.d.ts +60 -0
  28. package/dist/integrations/github/config.d.ts.map +1 -0
  29. package/dist/integrations/github/config.js +42 -0
  30. package/dist/integrations/github/config.js.map +1 -0
  31. package/dist/integrations/github/factory-session.d.ts +12 -0
  32. package/dist/integrations/github/factory-session.d.ts.map +1 -0
  33. package/dist/integrations/github/factory-session.js +37 -0
  34. package/dist/integrations/github/factory-session.js.map +1 -0
  35. package/dist/integrations/github/integration.d.ts +191 -0
  36. package/dist/integrations/github/integration.d.ts.map +1 -0
  37. package/dist/integrations/github/integration.js +1077 -0
  38. package/dist/integrations/github/integration.js.map +1 -0
  39. package/dist/integrations/github/issue-triage.d.ts +27 -0
  40. package/dist/integrations/github/issue-triage.d.ts.map +1 -0
  41. package/dist/integrations/github/issue-triage.js +82 -0
  42. package/dist/integrations/github/issue-triage.js.map +1 -0
  43. package/dist/integrations/github/pat.d.ts +38 -0
  44. package/dist/integrations/github/pat.d.ts.map +1 -0
  45. package/dist/integrations/github/pat.js +59 -0
  46. package/dist/integrations/github/pat.js.map +1 -0
  47. package/dist/integrations/github/provenance.d.ts +27 -0
  48. package/dist/integrations/github/provenance.d.ts.map +1 -0
  49. package/dist/integrations/github/provenance.js +75 -0
  50. package/dist/integrations/github/provenance.js.map +1 -0
  51. package/dist/integrations/github/routes.d.ts +69 -0
  52. package/dist/integrations/github/routes.d.ts.map +1 -0
  53. package/dist/integrations/github/routes.js +1324 -0
  54. package/dist/integrations/github/routes.js.map +1 -0
  55. package/dist/integrations/github/rules.d.ts +78 -0
  56. package/dist/integrations/github/rules.d.ts.map +1 -0
  57. package/dist/integrations/github/rules.js +362 -0
  58. package/dist/integrations/github/rules.js.map +1 -0
  59. package/dist/integrations/github/sandbox-release.d.ts +38 -0
  60. package/dist/integrations/github/sandbox-release.d.ts.map +1 -0
  61. package/dist/integrations/github/sandbox-release.js +74 -0
  62. package/dist/integrations/github/sandbox-release.js.map +1 -0
  63. package/dist/integrations/github/sandbox.d.ts +281 -0
  64. package/dist/integrations/github/sandbox.d.ts.map +1 -0
  65. package/dist/integrations/github/sandbox.js +547 -0
  66. package/dist/integrations/github/sandbox.js.map +1 -0
  67. package/dist/integrations/github/session-subscriptions.d.ts +26 -0
  68. package/dist/integrations/github/session-subscriptions.d.ts.map +1 -0
  69. package/dist/integrations/github/session-subscriptions.js +192 -0
  70. package/dist/integrations/github/session-subscriptions.js.map +1 -0
  71. package/dist/integrations/github/subscriptions.d.ts +54 -0
  72. package/dist/integrations/github/subscriptions.d.ts.map +1 -0
  73. package/dist/integrations/github/subscriptions.js +62 -0
  74. package/dist/integrations/github/subscriptions.js.map +1 -0
  75. package/dist/integrations/github/token-refresh.d.ts +12 -0
  76. package/dist/integrations/github/token-refresh.d.ts.map +1 -0
  77. package/dist/integrations/github/token-refresh.js +24 -0
  78. package/dist/integrations/github/token-refresh.js.map +1 -0
  79. package/dist/integrations/github/webhook.d.ts +87 -0
  80. package/dist/integrations/github/webhook.d.ts.map +1 -0
  81. package/dist/integrations/github/webhook.js +397 -0
  82. package/dist/integrations/github/webhook.js.map +1 -0
  83. package/dist/integrations/linear/agent-tools.d.ts +34 -0
  84. package/dist/integrations/linear/agent-tools.d.ts.map +1 -0
  85. package/dist/integrations/linear/agent-tools.js +85 -0
  86. package/dist/integrations/linear/agent-tools.js.map +1 -0
  87. package/dist/integrations/linear/integration.d.ts +233 -0
  88. package/dist/integrations/linear/integration.d.ts.map +1 -0
  89. package/dist/integrations/linear/integration.js +672 -0
  90. package/dist/integrations/linear/integration.js.map +1 -0
  91. package/dist/integrations/linear/routes.d.ts +61 -0
  92. package/dist/integrations/linear/routes.d.ts.map +1 -0
  93. package/dist/integrations/linear/routes.js +239 -0
  94. package/dist/integrations/linear/routes.js.map +1 -0
  95. package/dist/integrations/linear/rules.d.ts +42 -0
  96. package/dist/integrations/linear/rules.d.ts.map +1 -0
  97. package/dist/integrations/linear/rules.js +133 -0
  98. package/dist/integrations/linear/rules.js.map +1 -0
  99. package/dist/integrations/linear/storage.d.ts +65 -0
  100. package/dist/integrations/linear/storage.d.ts.map +1 -0
  101. package/dist/integrations/linear/storage.js +1 -0
  102. package/dist/integrations/platform/api-client.d.ts +26 -0
  103. package/dist/integrations/platform/api-client.d.ts.map +1 -0
  104. package/dist/integrations/platform/api-client.js +156 -0
  105. package/dist/integrations/platform/api-client.js.map +1 -0
  106. package/dist/integrations/platform/github/event-worker.d.ts +49 -0
  107. package/dist/integrations/platform/github/event-worker.d.ts.map +1 -0
  108. package/dist/integrations/platform/github/event-worker.js +348 -0
  109. package/dist/integrations/platform/github/event-worker.js.map +1 -0
  110. package/dist/integrations/platform/github/integration.d.ts +50 -0
  111. package/dist/integrations/platform/github/integration.d.ts.map +1 -0
  112. package/dist/integrations/platform/github/integration.js +961 -0
  113. package/dist/integrations/platform/github/integration.js.map +1 -0
  114. package/dist/integrations/platform/linear/integration.d.ts +46 -0
  115. package/dist/integrations/platform/linear/integration.d.ts.map +1 -0
  116. package/dist/integrations/platform/linear/integration.js +483 -0
  117. package/dist/integrations/platform/linear/integration.js.map +1 -0
  118. package/dist/integrations/workos/integration.d.ts +39 -0
  119. package/dist/integrations/workos/integration.d.ts.map +1 -0
  120. package/dist/integrations/workos/integration.js +91 -0
  121. package/dist/integrations/workos/integration.js.map +1 -0
  122. package/dist/routes/config.d.ts +235 -0
  123. package/dist/routes/config.d.ts.map +1 -0
  124. package/dist/routes/config.js +972 -0
  125. package/dist/routes/config.js.map +1 -0
  126. package/dist/routes/custom-provider-source.d.ts +52 -0
  127. package/dist/routes/custom-provider-source.d.ts.map +1 -0
  128. package/dist/routes/custom-provider-source.js +124 -0
  129. package/dist/routes/custom-provider-source.js.map +1 -0
  130. package/dist/routes/fs.d.ts +151 -0
  131. package/dist/routes/fs.d.ts.map +1 -0
  132. package/dist/routes/fs.js +708 -0
  133. package/dist/routes/fs.js.map +1 -0
  134. package/dist/routes/intake.d.ts +23 -0
  135. package/dist/routes/intake.d.ts.map +1 -0
  136. package/dist/routes/intake.js +179 -0
  137. package/dist/routes/intake.js.map +1 -0
  138. package/dist/routes/oauth.d.ts +43 -0
  139. package/dist/routes/oauth.d.ts.map +1 -0
  140. package/dist/routes/oauth.js +463 -0
  141. package/dist/routes/oauth.js.map +1 -0
  142. package/dist/routes/projects.d.ts +19 -0
  143. package/dist/routes/projects.d.ts.map +1 -0
  144. package/dist/routes/projects.js +461 -0
  145. package/dist/routes/projects.js.map +1 -0
  146. package/dist/routes/provider-credentials.d.ts +81 -0
  147. package/dist/routes/provider-credentials.d.ts.map +1 -0
  148. package/dist/routes/provider-credentials.js +86 -0
  149. package/dist/routes/provider-credentials.js.map +1 -0
  150. package/dist/routes/route.d.ts +48 -0
  151. package/dist/routes/route.d.ts.map +1 -0
  152. package/dist/routes/route.js +15 -0
  153. package/dist/routes/route.js.map +1 -0
  154. package/dist/routes/skills.d.ts +30 -0
  155. package/dist/routes/skills.d.ts.map +1 -0
  156. package/dist/routes/skills.js +168 -0
  157. package/dist/routes/skills.js.map +1 -0
  158. package/dist/routes/surface.d.ts +91 -0
  159. package/dist/routes/surface.d.ts.map +1 -0
  160. package/dist/routes/surface.js +260 -0
  161. package/dist/routes/surface.js.map +1 -0
  162. package/dist/routes/tenant-credentials.d.ts +74 -0
  163. package/dist/routes/tenant-credentials.d.ts.map +1 -0
  164. package/dist/routes/tenant-credentials.js +156 -0
  165. package/dist/routes/tenant-credentials.js.map +1 -0
  166. package/dist/routes/work-items.d.ts +40 -0
  167. package/dist/routes/work-items.d.ts.map +1 -0
  168. package/dist/routes/work-items.js +695 -0
  169. package/dist/routes/work-items.js.map +1 -0
  170. package/dist/rules/binding-context.d.ts +5 -0
  171. package/dist/rules/binding-context.d.ts.map +1 -0
  172. package/dist/rules/binding-context.js +28 -0
  173. package/dist/rules/binding-context.js.map +1 -0
  174. package/dist/rules/defaults.d.ts +9 -0
  175. package/dist/rules/defaults.d.ts.map +1 -0
  176. package/dist/rules/defaults.js +260 -0
  177. package/dist/rules/defaults.js.map +1 -0
  178. package/dist/rules/dispatcher.d.ts +42 -0
  179. package/dist/rules/dispatcher.d.ts.map +1 -0
  180. package/dist/rules/dispatcher.js +518 -0
  181. package/dist/rules/dispatcher.js.map +1 -0
  182. package/dist/rules/index.d.ts +7 -0
  183. package/dist/rules/index.d.ts.map +1 -0
  184. package/dist/rules/index.js +5 -0
  185. package/dist/rules/processor.d.ts +54 -0
  186. package/dist/rules/processor.d.ts.map +1 -0
  187. package/dist/rules/processor.js +378 -0
  188. package/dist/rules/processor.js.map +1 -0
  189. package/dist/rules/resolve.d.ts +17 -0
  190. package/dist/rules/resolve.d.ts.map +1 -0
  191. package/dist/rules/resolve.js +30 -0
  192. package/dist/rules/resolve.js.map +1 -0
  193. package/dist/rules/start-coordinator.d.ts +60 -0
  194. package/dist/rules/start-coordinator.d.ts.map +1 -0
  195. package/dist/rules/start-coordinator.js +194 -0
  196. package/dist/rules/start-coordinator.js.map +1 -0
  197. package/dist/rules/tools.d.ts +10 -0
  198. package/dist/rules/tools.d.ts.map +1 -0
  199. package/dist/rules/tools.js +58 -0
  200. package/dist/rules/tools.js.map +1 -0
  201. package/dist/rules/transition-service.d.ts +49 -0
  202. package/dist/rules/transition-service.d.ts.map +1 -0
  203. package/dist/rules/transition-service.js +232 -0
  204. package/dist/rules/transition-service.js.map +1 -0
  205. package/dist/rules/types.d.ts +234 -0
  206. package/dist/rules/types.d.ts.map +1 -0
  207. package/dist/rules/types.js +38 -0
  208. package/dist/rules/types.js.map +1 -0
  209. package/dist/rules/validation.d.ts +11 -0
  210. package/dist/rules/validation.d.ts.map +1 -0
  211. package/dist/rules/validation.js +308 -0
  212. package/dist/rules/validation.js.map +1 -0
  213. package/dist/sandbox/fleet.d.ts +222 -0
  214. package/dist/sandbox/fleet.d.ts.map +1 -0
  215. package/dist/sandbox/fleet.js +322 -0
  216. package/dist/sandbox/fleet.js.map +1 -0
  217. package/dist/sandbox/reattach.d.ts +3 -0
  218. package/dist/sandbox/reattach.d.ts.map +1 -0
  219. package/dist/sandbox/reattach.js +16 -0
  220. package/dist/sandbox/reattach.js.map +1 -0
  221. package/dist/server-error.d.ts +12 -0
  222. package/dist/server-error.d.ts.map +1 -0
  223. package/dist/server-error.js +16 -0
  224. package/dist/server-error.js.map +1 -0
  225. package/dist/skills/service.d.ts +48 -0
  226. package/dist/skills/service.d.ts.map +1 -0
  227. package/dist/skills/service.js +40 -0
  228. package/dist/skills/service.js.map +1 -0
  229. package/dist/spa-static.d.ts +33 -0
  230. package/dist/spa-static.d.ts.map +1 -0
  231. package/dist/spa-static.js +104 -0
  232. package/dist/spa-static.js.map +1 -0
  233. package/dist/state-signing.d.ts +59 -0
  234. package/dist/state-signing.d.ts.map +1 -0
  235. package/dist/state-signing.js +83 -0
  236. package/dist/state-signing.js.map +1 -0
  237. package/dist/storage/domains/audit/agent-audit.d.ts +27 -0
  238. package/dist/storage/domains/audit/agent-audit.d.ts.map +1 -0
  239. package/dist/storage/domains/audit/agent-audit.js +66 -0
  240. package/dist/storage/domains/audit/agent-audit.js.map +1 -0
  241. package/dist/storage/domains/audit/base.d.ts +140 -0
  242. package/dist/storage/domains/audit/base.d.ts.map +1 -0
  243. package/dist/storage/domains/audit/base.js +184 -0
  244. package/dist/storage/domains/audit/base.js.map +1 -0
  245. package/dist/storage/domains/audit/domain.d.ts +69 -0
  246. package/dist/storage/domains/audit/domain.d.ts.map +1 -0
  247. package/dist/storage/domains/audit/domain.js +167 -0
  248. package/dist/storage/domains/audit/domain.js.map +1 -0
  249. package/dist/storage/domains/channel-identity/base.d.ts +96 -0
  250. package/dist/storage/domains/channel-identity/base.d.ts.map +1 -0
  251. package/dist/storage/domains/channel-identity/base.js +149 -0
  252. package/dist/storage/domains/channel-identity/base.js.map +1 -0
  253. package/dist/storage/domains/credentials/base.d.ts +134 -0
  254. package/dist/storage/domains/credentials/base.d.ts.map +1 -0
  255. package/dist/storage/domains/credentials/base.js +273 -0
  256. package/dist/storage/domains/credentials/base.js.map +1 -0
  257. package/dist/storage/domains/custom-providers/base.d.ts +57 -0
  258. package/dist/storage/domains/custom-providers/base.d.ts.map +1 -0
  259. package/dist/storage/domains/custom-providers/base.js +145 -0
  260. package/dist/storage/domains/custom-providers/base.js.map +1 -0
  261. package/dist/storage/domains/intake/base.d.ts +38 -0
  262. package/dist/storage/domains/intake/base.d.ts.map +1 -0
  263. package/dist/storage/domains/intake/base.js +80 -0
  264. package/dist/storage/domains/intake/base.js.map +1 -0
  265. package/dist/storage/domains/integrations/base.d.ts +126 -0
  266. package/dist/storage/domains/integrations/base.d.ts.map +1 -0
  267. package/dist/storage/domains/integrations/base.js +317 -0
  268. package/dist/storage/domains/integrations/base.js.map +1 -0
  269. package/dist/storage/domains/memory-settings/base.d.ts +62 -0
  270. package/dist/storage/domains/memory-settings/base.d.ts.map +1 -0
  271. package/dist/storage/domains/memory-settings/base.js +126 -0
  272. package/dist/storage/domains/memory-settings/base.js.map +1 -0
  273. package/dist/storage/domains/model-packs/base.d.ts +49 -0
  274. package/dist/storage/domains/model-packs/base.d.ts.map +1 -0
  275. package/dist/storage/domains/model-packs/base.js +96 -0
  276. package/dist/storage/domains/model-packs/base.js.map +1 -0
  277. package/dist/storage/domains/projects/base.d.ts +58 -0
  278. package/dist/storage/domains/projects/base.d.ts.map +1 -0
  279. package/dist/storage/domains/projects/base.js +111 -0
  280. package/dist/storage/domains/projects/base.js.map +1 -0
  281. package/dist/storage/domains/queue-health/base.d.ts +52 -0
  282. package/dist/storage/domains/queue-health/base.d.ts.map +1 -0
  283. package/dist/storage/domains/queue-health/base.js +122 -0
  284. package/dist/storage/domains/queue-health/base.js.map +1 -0
  285. package/dist/storage/domains/source-control/base.d.ts +358 -0
  286. package/dist/storage/domains/source-control/base.d.ts.map +1 -0
  287. package/dist/storage/domains/source-control/base.js +908 -0
  288. package/dist/storage/domains/source-control/base.js.map +1 -0
  289. package/dist/storage/domains/source-control/inmemory.d.ts +175 -0
  290. package/dist/storage/domains/source-control/inmemory.d.ts.map +1 -0
  291. package/dist/storage/domains/source-control/inmemory.js +377 -0
  292. package/dist/storage/domains/source-control/inmemory.js.map +1 -0
  293. package/dist/storage/domains/work-items/base.d.ts +412 -0
  294. package/dist/storage/domains/work-items/base.d.ts.map +1 -0
  295. package/dist/storage/domains/work-items/base.js +1309 -0
  296. package/dist/storage/domains/work-items/base.js.map +1 -0
  297. package/dist/storage/domains/work-items/metrics.d.ts +101 -0
  298. package/dist/storage/domains/work-items/metrics.d.ts.map +1 -0
  299. package/dist/storage/domains/work-items/metrics.js +221 -0
  300. package/dist/storage/domains/work-items/metrics.js.map +1 -0
  301. package/dist/timing.d.ts +15 -0
  302. package/dist/timing.d.ts.map +1 -0
  303. package/dist/timing.js +34 -0
  304. package/dist/timing.js.map +1 -0
  305. package/dist/workspace.d.ts +24 -0
  306. package/dist/workspace.d.ts.map +1 -0
  307. package/dist/workspace.js +286 -0
  308. package/dist/workspace.js.map +1 -0
  309. package/factory-skills/configure-factory-rules/SKILL.md +67 -0
  310. package/factory-skills/factory-plan/SKILL.md +61 -0
  311. package/factory-skills/factory-review/SKILL.md +155 -0
  312. package/factory-skills/factory-triage/SKILL.md +77 -0
  313. package/package.json +81 -0
@@ -0,0 +1,908 @@
1
+ import { FactoryStorageDomain, UniqueViolationError } from "@mastra/core/storage";
2
+ //#region src/storage/domains/source-control/base.ts
3
+ const FACTORY_PROJECTS = "factory_projects";
4
+ const INSTALLATIONS = "source_control_installations";
5
+ const REPOSITORIES = "source_control_repositories";
6
+ const CONNECTIONS = "factory_project_source_control_connections";
7
+ const PROJECT_REPOSITORIES = "factory_project_repositories";
8
+ const SANDBOXES = "source_control_project_repository_sandboxes";
9
+ const SANDBOX_POOL = "source_control_sandbox_pool";
10
+ const WORKTREES = "source_control_worktrees";
11
+ const SESSIONS = "source_control_sessions";
12
+ const SOURCE_CONTROL_SCHEMAS = [
13
+ {
14
+ name: INSTALLATIONS,
15
+ columns: {
16
+ id: { type: "uuid-pk" },
17
+ integration_id: { type: "text" },
18
+ org_id: { type: "text" },
19
+ connected_by_user_id: { type: "text" },
20
+ external_id: { type: "text" },
21
+ account_name: {
22
+ type: "text",
23
+ nullable: true
24
+ },
25
+ account_type: {
26
+ type: "text",
27
+ nullable: true
28
+ },
29
+ provider_metadata: { type: "json" },
30
+ created_at: { type: "timestamp" }
31
+ },
32
+ uniqueIndexes: [{
33
+ name: "source_control_installations_integration_org_external_unique",
34
+ columns: [
35
+ "integration_id",
36
+ "org_id",
37
+ "external_id"
38
+ ]
39
+ }]
40
+ },
41
+ {
42
+ name: REPOSITORIES,
43
+ columns: {
44
+ id: { type: "uuid-pk" },
45
+ installation_id: { type: "text" },
46
+ external_id: { type: "text" },
47
+ slug: { type: "text" },
48
+ default_branch: {
49
+ type: "text",
50
+ default: "main"
51
+ },
52
+ provider_metadata: { type: "json" },
53
+ created_at: { type: "timestamp" },
54
+ updated_at: { type: "timestamp" }
55
+ },
56
+ uniqueIndexes: [{
57
+ name: "source_control_repositories_installation_external_unique",
58
+ columns: ["installation_id", "external_id"]
59
+ }],
60
+ indexes: [{
61
+ name: "source_control_repositories_installation_slug_idx",
62
+ columns: ["installation_id", "slug"]
63
+ }]
64
+ },
65
+ {
66
+ name: CONNECTIONS,
67
+ columns: {
68
+ id: { type: "uuid-pk" },
69
+ factory_project_id: { type: "text" },
70
+ integration_id: { type: "text" },
71
+ installation_id: { type: "text" },
72
+ created_by_user_id: { type: "text" },
73
+ created_at: { type: "timestamp" }
74
+ },
75
+ uniqueIndexes: [{
76
+ name: "factory_project_source_control_connections_project_integration_installation_unique",
77
+ columns: [
78
+ "factory_project_id",
79
+ "integration_id",
80
+ "installation_id"
81
+ ]
82
+ }],
83
+ indexes: [{
84
+ name: "factory_project_source_control_connections_project_idx",
85
+ columns: ["factory_project_id"]
86
+ }]
87
+ },
88
+ {
89
+ name: PROJECT_REPOSITORIES,
90
+ columns: {
91
+ id: { type: "uuid-pk" },
92
+ connection_id: { type: "text" },
93
+ repository_id: { type: "text" },
94
+ created_by_user_id: { type: "text" },
95
+ branch: {
96
+ type: "text",
97
+ nullable: true
98
+ },
99
+ sandbox_provider: { type: "text" },
100
+ sandbox_workdir: { type: "text" },
101
+ setup_command: {
102
+ type: "text",
103
+ nullable: true
104
+ },
105
+ created_at: { type: "timestamp" },
106
+ updated_at: { type: "timestamp" }
107
+ },
108
+ uniqueIndexes: [{
109
+ name: "factory_project_repositories_connection_repository_unique",
110
+ columns: ["connection_id", "repository_id"]
111
+ }],
112
+ indexes: [{
113
+ name: "factory_project_repositories_connection_idx",
114
+ columns: ["connection_id"]
115
+ }]
116
+ },
117
+ {
118
+ name: SANDBOXES,
119
+ columns: {
120
+ id: { type: "uuid-pk" },
121
+ project_repository_id: { type: "text" },
122
+ user_id: { type: "text" },
123
+ sandbox_id: {
124
+ type: "text",
125
+ nullable: true
126
+ },
127
+ sandbox_workdir: { type: "text" },
128
+ materialized_at: {
129
+ type: "timestamp",
130
+ nullable: true
131
+ },
132
+ created_at: { type: "timestamp" }
133
+ },
134
+ uniqueIndexes: [{
135
+ name: "source_control_project_repository_sandboxes_link_user_unique",
136
+ columns: ["project_repository_id", "user_id"]
137
+ }]
138
+ },
139
+ {
140
+ name: SANDBOX_POOL,
141
+ columns: {
142
+ id: { type: "uuid-pk" },
143
+ org_id: { type: "text" },
144
+ project_repository_id: { type: "text" },
145
+ user_id: { type: "text" },
146
+ sandbox_id: { type: "text" },
147
+ sandbox_workdir: { type: "text" },
148
+ released_at: { type: "timestamp" }
149
+ },
150
+ uniqueIndexes: [{
151
+ name: "source_control_sandbox_pool_sandbox_unique",
152
+ columns: ["sandbox_id"]
153
+ }],
154
+ indexes: [{
155
+ name: "source_control_sandbox_pool_repository_user_idx",
156
+ columns: ["project_repository_id", "user_id"]
157
+ }]
158
+ },
159
+ {
160
+ name: WORKTREES,
161
+ columns: {
162
+ id: { type: "uuid-pk" },
163
+ project_repository_id: { type: "text" },
164
+ user_id: { type: "text" },
165
+ branch: { type: "text" },
166
+ base_branch: { type: "text" },
167
+ worktree_path: { type: "text" },
168
+ created_at: { type: "timestamp" }
169
+ },
170
+ uniqueIndexes: [{
171
+ name: "source_control_worktrees_project_repository_user_branch_unique",
172
+ columns: [
173
+ "project_repository_id",
174
+ "user_id",
175
+ "branch"
176
+ ]
177
+ }]
178
+ },
179
+ {
180
+ name: SESSIONS,
181
+ columns: {
182
+ id: { type: "uuid-pk" },
183
+ session_id: { type: "text" },
184
+ project_repository_id: { type: "text" },
185
+ org_id: { type: "text" },
186
+ user_id: { type: "text" },
187
+ branch: { type: "text" },
188
+ base_branch: { type: "text" },
189
+ sandbox_id: {
190
+ type: "text",
191
+ nullable: true
192
+ },
193
+ sandbox_workdir: {
194
+ type: "text",
195
+ nullable: true
196
+ },
197
+ materialized_at: {
198
+ type: "timestamp",
199
+ nullable: true
200
+ },
201
+ created_at: { type: "timestamp" },
202
+ updated_at: { type: "timestamp" }
203
+ },
204
+ uniqueIndexes: [{
205
+ name: "source_control_sessions_session_id_unique",
206
+ columns: ["session_id"]
207
+ }, {
208
+ name: "source_control_sessions_repository_user_branch_unique",
209
+ columns: [
210
+ "project_repository_id",
211
+ "user_id",
212
+ "branch"
213
+ ]
214
+ }]
215
+ }
216
+ ];
217
+ function toInstallation(row) {
218
+ return {
219
+ id: row.id,
220
+ integrationId: row.integration_id,
221
+ orgId: row.org_id,
222
+ connectedByUserId: row.connected_by_user_id,
223
+ externalId: row.external_id,
224
+ accountName: row.account_name,
225
+ accountType: row.account_type,
226
+ providerMetadata: row.provider_metadata,
227
+ createdAt: row.created_at
228
+ };
229
+ }
230
+ function toRepository(row) {
231
+ return {
232
+ id: row.id,
233
+ installationId: row.installation_id,
234
+ externalId: row.external_id,
235
+ slug: row.slug,
236
+ defaultBranch: row.default_branch,
237
+ providerMetadata: row.provider_metadata,
238
+ createdAt: row.created_at,
239
+ updatedAt: row.updated_at
240
+ };
241
+ }
242
+ function toConnection(row) {
243
+ return {
244
+ id: row.id,
245
+ factoryProjectId: row.factory_project_id,
246
+ integrationId: row.integration_id,
247
+ installationId: row.installation_id,
248
+ createdByUserId: row.created_by_user_id,
249
+ createdAt: row.created_at
250
+ };
251
+ }
252
+ function toProjectRepository(row) {
253
+ return {
254
+ id: row.id,
255
+ connectionId: row.connection_id,
256
+ repositoryId: row.repository_id,
257
+ createdByUserId: row.created_by_user_id,
258
+ branch: row.branch,
259
+ sandboxProvider: row.sandbox_provider,
260
+ sandboxWorkdir: row.sandbox_workdir,
261
+ setupCommand: row.setup_command,
262
+ createdAt: row.created_at,
263
+ updatedAt: row.updated_at
264
+ };
265
+ }
266
+ function toSandbox(row) {
267
+ return {
268
+ id: row.id,
269
+ projectRepositoryId: row.project_repository_id,
270
+ userId: row.user_id,
271
+ sandboxId: row.sandbox_id,
272
+ sandboxWorkdir: row.sandbox_workdir,
273
+ materializedAt: row.materialized_at,
274
+ createdAt: row.created_at
275
+ };
276
+ }
277
+ function toPooledSandbox(row) {
278
+ return {
279
+ id: row.id,
280
+ orgId: row.org_id,
281
+ projectRepositoryId: row.project_repository_id,
282
+ userId: row.user_id,
283
+ sandboxId: row.sandbox_id,
284
+ sandboxWorkdir: row.sandbox_workdir,
285
+ releasedAt: row.released_at
286
+ };
287
+ }
288
+ function toWorktree(row) {
289
+ return {
290
+ id: row.id,
291
+ projectRepositoryId: row.project_repository_id,
292
+ userId: row.user_id,
293
+ branch: row.branch,
294
+ baseBranch: row.base_branch,
295
+ worktreePath: row.worktree_path,
296
+ createdAt: row.created_at
297
+ };
298
+ }
299
+ function toSession(row) {
300
+ return {
301
+ id: row.id,
302
+ sessionId: row.session_id,
303
+ projectRepositoryId: row.project_repository_id,
304
+ orgId: row.org_id,
305
+ userId: row.user_id,
306
+ branch: row.branch,
307
+ baseBranch: row.base_branch,
308
+ sandboxId: row.sandbox_id,
309
+ sandboxWorkdir: row.sandbox_workdir,
310
+ materializedAt: row.materialized_at,
311
+ createdAt: row.created_at,
312
+ updatedAt: row.updated_at
313
+ };
314
+ }
315
+ var SourceControlStorage = class extends FactoryStorageDomain {
316
+ constructor() {
317
+ super("source-control");
318
+ }
319
+ async init() {
320
+ await this.ensureCollections(SOURCE_CONTROL_SCHEMAS);
321
+ }
322
+ async dangerouslyClearAll() {
323
+ await this.ops.deleteMany(SESSIONS, {});
324
+ await this.ops.deleteMany(WORKTREES, {});
325
+ await this.ops.deleteMany(SANDBOX_POOL, {});
326
+ await this.ops.deleteMany(SANDBOXES, {});
327
+ await this.ops.deleteMany(PROJECT_REPOSITORIES, {});
328
+ await this.ops.deleteMany(CONNECTIONS, {});
329
+ await this.ops.deleteMany(REPOSITORIES, {});
330
+ await this.ops.deleteMany(INSTALLATIONS, {});
331
+ }
332
+ forIntegration(integrationId) {
333
+ if (!integrationId.trim()) throw new Error("[SourceControlStorage] integrationId must not be empty.");
334
+ const db = () => this.ops;
335
+ const getInstallation = async (args) => {
336
+ const row = await db().findOne(INSTALLATIONS, {
337
+ id: args.id,
338
+ integration_id: integrationId,
339
+ org_id: args.orgId
340
+ });
341
+ return row ? toInstallation(row) : null;
342
+ };
343
+ const requireInstallation = async (args) => {
344
+ const installation = await getInstallation(args);
345
+ if (!installation) throw new Error("Source-control installation not found for this organization and integration.");
346
+ return installation;
347
+ };
348
+ const getRepository = async (args) => {
349
+ const row = await db().findOne(REPOSITORIES, { id: args.id });
350
+ if (!row || !await getInstallation({
351
+ orgId: args.orgId,
352
+ id: row.installation_id
353
+ })) return null;
354
+ return toRepository(row);
355
+ };
356
+ const requireRepository = async (args) => {
357
+ const repository = await getRepository(args);
358
+ if (!repository) throw new Error("Source-control repository not found for this organization and integration.");
359
+ return repository;
360
+ };
361
+ const getConnection = async (args) => {
362
+ const row = await db().findOne(CONNECTIONS, {
363
+ id: args.id,
364
+ integration_id: integrationId
365
+ });
366
+ if (!row) return null;
367
+ if (!await db().findOne(FACTORY_PROJECTS, {
368
+ id: row.factory_project_id,
369
+ org_id: args.orgId
370
+ }) || !await getInstallation({
371
+ orgId: args.orgId,
372
+ id: row.installation_id
373
+ })) return null;
374
+ return toConnection(row);
375
+ };
376
+ const requireConnection = async (args) => {
377
+ const connection = await getConnection(args);
378
+ if (!connection) throw new Error("Project source-control connection not found for this organization and integration.");
379
+ return connection;
380
+ };
381
+ const getProjectRepository = async (args) => {
382
+ const row = await db().findOne(PROJECT_REPOSITORIES, { id: args.id });
383
+ if (!row || !await getConnection({
384
+ orgId: args.orgId,
385
+ id: row.connection_id
386
+ })) return null;
387
+ return toProjectRepository(row);
388
+ };
389
+ const getProjectRepositoryById = async (id) => {
390
+ const row = await db().findOne(PROJECT_REPOSITORIES, { id });
391
+ if (!row) return null;
392
+ return await db().findOne(CONNECTIONS, {
393
+ id: row.connection_id,
394
+ integration_id: integrationId
395
+ }) ? toProjectRepository(row) : null;
396
+ };
397
+ const requireProjectRepositoryById = async (id) => {
398
+ const projectRepository = await getProjectRepositoryById(id);
399
+ if (!projectRepository) throw new Error("Project repository not found for this integration.");
400
+ return projectRepository;
401
+ };
402
+ const getSandbox = async (id) => {
403
+ const row = await db().findOne(SANDBOXES, { id });
404
+ if (!row || !await getProjectRepositoryById(row.project_repository_id)) return null;
405
+ return toSandbox(row);
406
+ };
407
+ const requireSandbox = async (id) => {
408
+ const sandbox = await getSandbox(id);
409
+ if (!sandbox) throw new Error("Project-repository sandbox not found for this integration.");
410
+ return sandbox;
411
+ };
412
+ return {
413
+ integrationId,
414
+ installations: {
415
+ list: async ({ orgId }) => (await db().findMany(INSTALLATIONS, {
416
+ integration_id: integrationId,
417
+ org_id: orgId
418
+ })).map(toInstallation),
419
+ get: getInstallation,
420
+ findByExternalId: async ({ orgId, externalId }) => {
421
+ const row = await db().findOne(INSTALLATIONS, {
422
+ integration_id: integrationId,
423
+ org_id: orgId,
424
+ external_id: externalId
425
+ });
426
+ return row ? toInstallation(row) : null;
427
+ },
428
+ upsert: async (input) => {
429
+ return toInstallation(await db().upsertOne(INSTALLATIONS, [
430
+ "integration_id",
431
+ "org_id",
432
+ "external_id"
433
+ ], {
434
+ integration_id: integrationId,
435
+ org_id: input.orgId,
436
+ connected_by_user_id: input.connectedByUserId,
437
+ external_id: input.externalId,
438
+ account_name: input.accountName ?? null,
439
+ account_type: input.accountType ?? null,
440
+ provider_metadata: input.providerMetadata ?? {},
441
+ created_at: /* @__PURE__ */ new Date()
442
+ }));
443
+ },
444
+ delete: async ({ orgId, id }) => {
445
+ if (!await getInstallation({
446
+ orgId,
447
+ id
448
+ })) return false;
449
+ await db().deleteMany(INSTALLATIONS, {
450
+ id,
451
+ integration_id: integrationId,
452
+ org_id: orgId
453
+ });
454
+ return true;
455
+ }
456
+ },
457
+ repositories: {
458
+ list: async ({ orgId, installationId }) => {
459
+ await requireInstallation({
460
+ orgId,
461
+ id: installationId
462
+ });
463
+ return (await db().findMany(REPOSITORIES, { installation_id: installationId })).map(toRepository);
464
+ },
465
+ get: getRepository,
466
+ findByExternalId: async ({ orgId, externalId }) => {
467
+ const rows = await db().findMany(REPOSITORIES, { external_id: externalId });
468
+ for (const row of rows) if (await getInstallation({
469
+ orgId,
470
+ id: row.installation_id
471
+ })) return toRepository(row);
472
+ return null;
473
+ },
474
+ findBySlug: async ({ orgId, installationId, slug }) => {
475
+ await requireInstallation({
476
+ orgId,
477
+ id: installationId
478
+ });
479
+ const row = await db().findOne(REPOSITORIES, {
480
+ installation_id: installationId,
481
+ slug
482
+ });
483
+ return row ? toRepository(row) : null;
484
+ },
485
+ upsert: async ({ orgId, input }) => {
486
+ await requireInstallation({
487
+ orgId,
488
+ id: input.installationId
489
+ });
490
+ const now = /* @__PURE__ */ new Date();
491
+ return toRepository(await db().upsertOne(REPOSITORIES, ["installation_id", "external_id"], {
492
+ installation_id: input.installationId,
493
+ external_id: input.externalId,
494
+ slug: input.slug,
495
+ default_branch: input.defaultBranch,
496
+ provider_metadata: input.providerMetadata ?? {},
497
+ created_at: now,
498
+ updated_at: now
499
+ }));
500
+ },
501
+ migrateInstallation: async ({ orgId, id, newInstallationId }) => {
502
+ const existing = await getRepository({
503
+ orgId,
504
+ id
505
+ });
506
+ if (!existing) throw new Error(`Repository ${id} not found in organization ${orgId}`);
507
+ await requireInstallation({
508
+ orgId,
509
+ id: newInstallationId
510
+ });
511
+ try {
512
+ await db().updateMany(REPOSITORIES, { id }, {
513
+ installation_id: newInstallationId,
514
+ updated_at: /* @__PURE__ */ new Date()
515
+ });
516
+ await db().updateMany(CONNECTIONS, { installation_id: existing.installationId }, { installation_id: newInstallationId });
517
+ const updated = await getRepository({
518
+ orgId,
519
+ id
520
+ });
521
+ if (!updated) throw new Error("Repository disappeared after update");
522
+ return updated;
523
+ } catch (error) {
524
+ if (!(error instanceof UniqueViolationError)) throw error;
525
+ const conflictRow = await db().findOne(REPOSITORIES, {
526
+ installation_id: newInstallationId,
527
+ external_id: existing.externalId
528
+ });
529
+ if (!conflictRow) throw error;
530
+ return toRepository(conflictRow);
531
+ }
532
+ }
533
+ },
534
+ connections: {
535
+ list: async ({ orgId, factoryProjectId }) => {
536
+ if (!await db().findOne(FACTORY_PROJECTS, {
537
+ id: factoryProjectId,
538
+ org_id: orgId
539
+ })) return [];
540
+ return (await db().findMany(CONNECTIONS, {
541
+ factory_project_id: factoryProjectId,
542
+ integration_id: integrationId
543
+ })).map(toConnection);
544
+ },
545
+ get: getConnection,
546
+ create: async (input) => {
547
+ if (!await db().findOne(FACTORY_PROJECTS, {
548
+ id: input.factoryProjectId,
549
+ org_id: input.orgId
550
+ })) throw new Error("Factory project not found for this organization.");
551
+ await requireInstallation({
552
+ orgId: input.orgId,
553
+ id: input.installationId
554
+ });
555
+ try {
556
+ return toConnection(await db().insertOne(CONNECTIONS, {
557
+ factory_project_id: input.factoryProjectId,
558
+ integration_id: integrationId,
559
+ installation_id: input.installationId,
560
+ created_by_user_id: input.createdByUserId,
561
+ created_at: /* @__PURE__ */ new Date()
562
+ }));
563
+ } catch (error) {
564
+ if (!(error instanceof UniqueViolationError)) throw error;
565
+ const row = await db().findOne(CONNECTIONS, {
566
+ factory_project_id: input.factoryProjectId,
567
+ integration_id: integrationId,
568
+ installation_id: input.installationId
569
+ });
570
+ if (!row) throw error;
571
+ return toConnection(row);
572
+ }
573
+ },
574
+ delete: async ({ orgId, id }) => {
575
+ if (!await getConnection({
576
+ orgId,
577
+ id
578
+ })) return false;
579
+ const projectRepositories = await db().findMany(PROJECT_REPOSITORIES, { connection_id: id });
580
+ for (const projectRepository of projectRepositories) {
581
+ await db().deleteMany(SESSIONS, { project_repository_id: projectRepository.id });
582
+ await db().deleteMany(WORKTREES, { project_repository_id: projectRepository.id });
583
+ await db().deleteMany(SANDBOX_POOL, { project_repository_id: projectRepository.id });
584
+ await db().deleteMany(SANDBOXES, { project_repository_id: projectRepository.id });
585
+ }
586
+ await db().deleteMany(PROJECT_REPOSITORIES, { connection_id: id });
587
+ await db().deleteMany(CONNECTIONS, {
588
+ id,
589
+ integration_id: integrationId
590
+ });
591
+ return true;
592
+ }
593
+ },
594
+ projectRepositories: {
595
+ list: async ({ orgId, connectionId }) => {
596
+ await requireConnection({
597
+ orgId,
598
+ id: connectionId
599
+ });
600
+ return (await db().findMany(PROJECT_REPOSITORIES, { connection_id: connectionId })).map(toProjectRepository);
601
+ },
602
+ listConfiguredExternalKeys: async () => {
603
+ const keys = /* @__PURE__ */ new Map();
604
+ const connections = await db().findMany(CONNECTIONS, { integration_id: integrationId });
605
+ for (const connection of connections) {
606
+ const installation = await db().findOne(INSTALLATIONS, {
607
+ id: connection.installation_id,
608
+ integration_id: integrationId
609
+ });
610
+ if (!installation) continue;
611
+ const links = await db().findMany(PROJECT_REPOSITORIES, { connection_id: connection.id });
612
+ for (const link of links) {
613
+ const repository = await db().findOne(REPOSITORIES, { id: link.repository_id });
614
+ if (!repository) continue;
615
+ keys.set(`${installation.external_id}\u0000${repository.external_id}`, {
616
+ installationExternalId: installation.external_id,
617
+ repositoryExternalId: repository.external_id
618
+ });
619
+ }
620
+ }
621
+ return [...keys.values()];
622
+ },
623
+ listByExternalRepository: async ({ installationExternalId, repositoryExternalId }) => {
624
+ const targets = [];
625
+ const installations = await db().findMany(INSTALLATIONS, {
626
+ integration_id: integrationId,
627
+ external_id: installationExternalId
628
+ });
629
+ for (const installation of installations) {
630
+ const repository = await db().findOne(REPOSITORIES, {
631
+ installation_id: installation.id,
632
+ external_id: repositoryExternalId
633
+ });
634
+ if (!repository) continue;
635
+ const links = await db().findMany(PROJECT_REPOSITORIES, { repository_id: repository.id });
636
+ for (const link of links) {
637
+ const connection = await db().findOne(CONNECTIONS, {
638
+ id: link.connection_id,
639
+ integration_id: integrationId,
640
+ installation_id: installation.id
641
+ });
642
+ if (!connection) continue;
643
+ if (!await db().findOne(FACTORY_PROJECTS, {
644
+ id: connection.factory_project_id,
645
+ org_id: installation.org_id
646
+ })) continue;
647
+ targets.push({
648
+ orgId: installation.org_id,
649
+ factoryProjectId: connection.factory_project_id,
650
+ projectRepository: toProjectRepository(link)
651
+ });
652
+ }
653
+ }
654
+ return targets;
655
+ },
656
+ get: getProjectRepository,
657
+ link: async (input) => {
658
+ const connection = await requireConnection({
659
+ orgId: input.orgId,
660
+ id: input.connectionId
661
+ });
662
+ if ((await requireRepository({
663
+ orgId: input.orgId,
664
+ id: input.repositoryId
665
+ })).installationId !== connection.installationId) throw new Error("Repository does not belong to the connection installation.");
666
+ const now = /* @__PURE__ */ new Date();
667
+ return toProjectRepository(await db().upsertOne(PROJECT_REPOSITORIES, ["connection_id", "repository_id"], {
668
+ connection_id: input.connectionId,
669
+ repository_id: input.repositoryId,
670
+ created_by_user_id: input.createdByUserId,
671
+ branch: input.branch ?? null,
672
+ sandbox_provider: input.sandboxProvider,
673
+ sandbox_workdir: input.sandboxWorkdir,
674
+ setup_command: input.setupCommand ?? null,
675
+ created_at: now,
676
+ updated_at: now
677
+ }));
678
+ },
679
+ update: async ({ orgId, id, input }) => {
680
+ if (!await getProjectRepository({
681
+ orgId,
682
+ id
683
+ })) return null;
684
+ const patch = { updated_at: /* @__PURE__ */ new Date() };
685
+ if (input.branch !== void 0) patch.branch = input.branch;
686
+ if (input.sandboxProvider !== void 0) patch.sandbox_provider = input.sandboxProvider;
687
+ if (input.sandboxWorkdir !== void 0) patch.sandbox_workdir = input.sandboxWorkdir;
688
+ if (input.setupCommand !== void 0) patch.setup_command = input.setupCommand;
689
+ await db().updateMany(PROJECT_REPOSITORIES, { id }, patch);
690
+ return getProjectRepository({
691
+ orgId,
692
+ id
693
+ });
694
+ },
695
+ unlink: async ({ orgId, id }) => {
696
+ if (!await getProjectRepository({
697
+ orgId,
698
+ id
699
+ })) return false;
700
+ await db().deleteMany(SESSIONS, { project_repository_id: id });
701
+ await db().deleteMany(WORKTREES, { project_repository_id: id });
702
+ await db().deleteMany(SANDBOX_POOL, { project_repository_id: id });
703
+ await db().deleteMany(SANDBOXES, { project_repository_id: id });
704
+ await db().deleteMany(PROJECT_REPOSITORIES, { id });
705
+ return true;
706
+ }
707
+ },
708
+ sandboxes: {
709
+ getOrCreate: async ({ projectRepository, userId }) => {
710
+ await requireProjectRepositoryById(projectRepository.id);
711
+ const where = {
712
+ project_repository_id: projectRepository.id,
713
+ user_id: userId
714
+ };
715
+ const existing = await db().findOne(SANDBOXES, where);
716
+ if (existing) return toSandbox(existing);
717
+ try {
718
+ return toSandbox(await db().insertOne(SANDBOXES, {
719
+ ...where,
720
+ sandbox_id: null,
721
+ sandbox_workdir: projectRepository.sandboxWorkdir,
722
+ materialized_at: null,
723
+ created_at: /* @__PURE__ */ new Date()
724
+ }));
725
+ } catch (error) {
726
+ if (!(error instanceof UniqueViolationError)) throw error;
727
+ const row = await db().findOne(SANDBOXES, where);
728
+ if (!row) throw error;
729
+ return toSandbox(row);
730
+ }
731
+ },
732
+ getById: ({ id }) => getSandbox(id),
733
+ setWorkdir: async ({ id, sandboxWorkdir }) => {
734
+ await requireSandbox(id);
735
+ await db().updateMany(SANDBOXES, { id }, {
736
+ sandbox_workdir: sandboxWorkdir,
737
+ materialized_at: null
738
+ });
739
+ },
740
+ setSandboxId: async ({ id, sandboxId }) => {
741
+ await requireSandbox(id);
742
+ await db().updateMany(SANDBOXES, { id }, { sandbox_id: sandboxId });
743
+ },
744
+ clearBinding: async ({ id }) => {
745
+ await requireSandbox(id);
746
+ await db().updateMany(SANDBOXES, { id }, {
747
+ sandbox_id: null,
748
+ materialized_at: null
749
+ });
750
+ },
751
+ markMaterialized: async ({ id }) => {
752
+ await requireSandbox(id);
753
+ await db().updateMany(SANDBOXES, { id }, { materialized_at: /* @__PURE__ */ new Date() });
754
+ }
755
+ },
756
+ sandboxPool: {
757
+ release: async (input) => {
758
+ if (!await getProjectRepositoryById(input.projectRepositoryId)) return;
759
+ try {
760
+ await db().insertOne(SANDBOX_POOL, {
761
+ org_id: input.orgId,
762
+ project_repository_id: input.projectRepositoryId,
763
+ user_id: input.userId,
764
+ sandbox_id: input.sandboxId,
765
+ sandbox_workdir: input.sandboxWorkdir,
766
+ released_at: /* @__PURE__ */ new Date()
767
+ });
768
+ } catch (error) {
769
+ if (!(error instanceof UniqueViolationError)) throw error;
770
+ }
771
+ },
772
+ claim: async ({ projectRepositoryId }) => {
773
+ if (!await getProjectRepositoryById(projectRepositoryId)) return null;
774
+ const rows = await db().findMany(SANDBOX_POOL, { project_repository_id: projectRepositoryId });
775
+ rows.sort((left, right) => right.released_at.getTime() - left.released_at.getTime());
776
+ for (const row of rows) if (await db().deleteMany(SANDBOX_POOL, { id: row.id }) === 1) return toPooledSandbox(row);
777
+ return null;
778
+ }
779
+ },
780
+ worktrees: {
781
+ upsert: async (input) => {
782
+ await requireProjectRepositoryById(input.projectRepositoryId);
783
+ await db().upsertOne(WORKTREES, [
784
+ "project_repository_id",
785
+ "user_id",
786
+ "branch"
787
+ ], {
788
+ project_repository_id: input.projectRepositoryId,
789
+ user_id: input.userId,
790
+ branch: input.branch,
791
+ base_branch: input.baseBranch,
792
+ worktree_path: input.worktreePath,
793
+ created_at: /* @__PURE__ */ new Date()
794
+ });
795
+ },
796
+ list: async ({ projectRepositoryId, userId }) => {
797
+ if (!await getProjectRepositoryById(projectRepositoryId)) return [];
798
+ return (await db().findMany(WORKTREES, {
799
+ project_repository_id: projectRepositoryId,
800
+ user_id: userId
801
+ })).map(toWorktree);
802
+ },
803
+ get: async ({ projectRepositoryId, userId, branch }) => {
804
+ if (!await getProjectRepositoryById(projectRepositoryId)) return null;
805
+ const row = await db().findOne(WORKTREES, {
806
+ project_repository_id: projectRepositoryId,
807
+ user_id: userId,
808
+ branch
809
+ });
810
+ return row ? toWorktree(row) : null;
811
+ },
812
+ findByPath: async ({ projectRepositoryId, userId, worktreePath }) => {
813
+ if (!await getProjectRepositoryById(projectRepositoryId)) return null;
814
+ const row = await db().findOne(WORKTREES, {
815
+ project_repository_id: projectRepositoryId,
816
+ user_id: userId,
817
+ worktree_path: worktreePath
818
+ });
819
+ return row ? toWorktree(row) : null;
820
+ },
821
+ delete: async ({ projectRepositoryId, userId, branch }) => {
822
+ await requireProjectRepositoryById(projectRepositoryId);
823
+ await db().deleteMany(WORKTREES, {
824
+ project_repository_id: projectRepositoryId,
825
+ user_id: userId,
826
+ branch
827
+ });
828
+ }
829
+ },
830
+ sessions: {
831
+ list: async ({ projectRepositoryId, userId }) => {
832
+ if (!await getProjectRepositoryById(projectRepositoryId)) return [];
833
+ return (await db().findMany(SESSIONS, {
834
+ project_repository_id: projectRepositoryId,
835
+ user_id: userId
836
+ })).map(toSession);
837
+ },
838
+ getBySessionId: async (sessionId) => {
839
+ const row = await db().findOne(SESSIONS, { session_id: sessionId });
840
+ return row && await getProjectRepositoryById(row.project_repository_id) ? toSession(row) : null;
841
+ },
842
+ getForBranch: async ({ projectRepositoryId, userId, branch }) => {
843
+ if (!await getProjectRepositoryById(projectRepositoryId)) return null;
844
+ const row = await db().findOne(SESSIONS, {
845
+ project_repository_id: projectRepositoryId,
846
+ user_id: userId,
847
+ branch
848
+ });
849
+ return row ? toSession(row) : null;
850
+ },
851
+ create: async (input) => {
852
+ await requireProjectRepositoryById(input.projectRepositoryId);
853
+ const existing = await db().findOne(SESSIONS, {
854
+ project_repository_id: input.projectRepositoryId,
855
+ user_id: input.userId,
856
+ branch: input.branch
857
+ });
858
+ if (existing) return toSession(existing);
859
+ const now = /* @__PURE__ */ new Date();
860
+ try {
861
+ return toSession(await db().insertOne(SESSIONS, {
862
+ session_id: input.sessionId,
863
+ project_repository_id: input.projectRepositoryId,
864
+ org_id: input.orgId,
865
+ user_id: input.userId,
866
+ branch: input.branch,
867
+ base_branch: input.baseBranch,
868
+ sandbox_id: null,
869
+ sandbox_workdir: null,
870
+ materialized_at: null,
871
+ created_at: now,
872
+ updated_at: now
873
+ }));
874
+ } catch (error) {
875
+ if (!(error instanceof UniqueViolationError)) throw error;
876
+ const row = await db().findOne(SESSIONS, {
877
+ project_repository_id: input.projectRepositoryId,
878
+ user_id: input.userId,
879
+ branch: input.branch
880
+ });
881
+ if (!row) throw error;
882
+ return toSession(row);
883
+ }
884
+ },
885
+ setSandbox: async ({ id, sandboxId, sandboxWorkdir }) => {
886
+ await db().updateMany(SESSIONS, { id }, {
887
+ sandbox_id: sandboxId,
888
+ sandbox_workdir: sandboxWorkdir,
889
+ updated_at: /* @__PURE__ */ new Date()
890
+ });
891
+ },
892
+ markMaterialized: async ({ id }) => {
893
+ await db().updateMany(SESSIONS, { id }, {
894
+ materialized_at: /* @__PURE__ */ new Date(),
895
+ updated_at: /* @__PURE__ */ new Date()
896
+ });
897
+ },
898
+ delete: async (id) => {
899
+ await db().deleteMany(SESSIONS, { id });
900
+ }
901
+ }
902
+ };
903
+ }
904
+ };
905
+ //#endregion
906
+ export { SOURCE_CONTROL_SCHEMAS, SourceControlStorage };
907
+
908
+ //# sourceMappingURL=base.js.map