@mastra/factory 0.28.0-alpha.2 → 0.28.0-alpha.4

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 (455) hide show
  1. package/README.md +329 -154
  2. package/dist/boards/board-events.d.ts +23 -3
  3. package/dist/boards/board-events.d.ts.map +1 -1
  4. package/dist/boards/board-events.js +44 -7
  5. package/dist/boards/board-events.js.map +1 -1
  6. package/dist/boards/board-writes.d.ts +1 -1
  7. package/dist/boards/board-writes.d.ts.map +1 -1
  8. package/dist/boards/board-writes.js +16 -1
  9. package/dist/boards/board-writes.js.map +1 -1
  10. package/dist/boards/card-facts.d.ts +3 -1
  11. package/dist/boards/card-facts.d.ts.map +1 -1
  12. package/dist/boards/card-facts.js +30 -4
  13. package/dist/boards/card-facts.js.map +1 -1
  14. package/dist/boards/default-board.d.ts +7 -0
  15. package/dist/boards/default-board.d.ts.map +1 -0
  16. package/dist/boards/default-board.js +10 -0
  17. package/dist/boards/default-board.js.map +1 -0
  18. package/dist/boards/define-board.d.ts +40 -18
  19. package/dist/boards/define-board.d.ts.map +1 -1
  20. package/dist/boards/define-board.js +64 -42
  21. package/dist/boards/define-board.js.map +1 -1
  22. package/dist/boards/index.d.ts +5 -8
  23. package/dist/boards/index.d.ts.map +1 -1
  24. package/dist/boards/index.js +5 -19
  25. package/dist/boards/intake-routing.d.ts +29 -9
  26. package/dist/boards/intake-routing.d.ts.map +1 -1
  27. package/dist/boards/intake-routing.js +72 -20
  28. package/dist/boards/intake-routing.js.map +1 -1
  29. package/dist/boards/legacy-plan-role.d.ts.map +1 -1
  30. package/dist/boards/legacy-plan-role.js +7 -2
  31. package/dist/boards/legacy-plan-role.js.map +1 -1
  32. package/dist/boards/phase-automation.d.ts +7 -0
  33. package/dist/boards/phase-automation.d.ts.map +1 -1
  34. package/dist/boards/phase-automation.js +16 -5
  35. package/dist/boards/phase-automation.js.map +1 -1
  36. package/dist/boards/phase-runner.d.ts +18 -10
  37. package/dist/boards/phase-runner.d.ts.map +1 -1
  38. package/dist/boards/phase-runner.js +39 -34
  39. package/dist/boards/phase-runner.js.map +1 -1
  40. package/dist/boards/project-boards.d.ts +27 -7
  41. package/dist/boards/project-boards.d.ts.map +1 -1
  42. package/dist/boards/project-boards.js +94 -19
  43. package/dist/boards/project-boards.js.map +1 -1
  44. package/dist/boards/registry.d.ts.map +1 -1
  45. package/dist/boards/registry.js +3 -1
  46. package/dist/boards/registry.js.map +1 -1
  47. package/dist/boards/relocate.d.ts +1 -1
  48. package/dist/boards/relocate.d.ts.map +1 -1
  49. package/dist/boards/relocate.js +3 -2
  50. package/dist/boards/relocate.js.map +1 -1
  51. package/dist/boards/review.d.ts +5 -2
  52. package/dist/boards/review.d.ts.map +1 -1
  53. package/dist/boards/review.js +12 -11
  54. package/dist/boards/review.js.map +1 -1
  55. package/dist/boards/semantics.d.ts +159 -20
  56. package/dist/boards/semantics.d.ts.map +1 -1
  57. package/dist/boards/semantics.js +254 -23
  58. package/dist/boards/semantics.js.map +1 -1
  59. package/dist/boards/stored-board.d.ts +32 -18
  60. package/dist/boards/stored-board.d.ts.map +1 -1
  61. package/dist/boards/stored-board.js +31 -97
  62. package/dist/boards/stored-board.js.map +1 -1
  63. package/dist/boards/work-migrations.d.ts +42 -0
  64. package/dist/boards/work-migrations.d.ts.map +1 -0
  65. package/dist/boards/work-migrations.js +352 -0
  66. package/dist/boards/work-migrations.js.map +1 -0
  67. package/dist/boards/work-routes.d.ts +58 -0
  68. package/dist/boards/work-routes.d.ts.map +1 -0
  69. package/dist/boards/work-routes.js +270 -0
  70. package/dist/boards/work-routes.js.map +1 -0
  71. package/dist/boards/work.d.ts +8 -19
  72. package/dist/boards/work.d.ts.map +1 -1
  73. package/dist/boards/work.js +38 -270
  74. package/dist/boards/work.js.map +1 -1
  75. package/dist/factory.d.ts +26 -3
  76. package/dist/factory.d.ts.map +1 -1
  77. package/dist/factory.js +105 -62
  78. package/dist/factory.js.map +1 -1
  79. package/dist/index.d.ts +5 -2
  80. package/dist/index.d.ts.map +1 -1
  81. package/dist/index.js +3 -3
  82. package/dist/integrations/base.d.ts +6 -2
  83. package/dist/integrations/base.d.ts.map +1 -1
  84. package/dist/integrations/closing-references.d.ts +15 -0
  85. package/dist/integrations/closing-references.d.ts.map +1 -0
  86. package/dist/integrations/closing-references.js +69 -0
  87. package/dist/integrations/closing-references.js.map +1 -0
  88. package/dist/integrations/github/default-rules.d.ts +15 -66
  89. package/dist/integrations/github/default-rules.d.ts.map +1 -1
  90. package/dist/integrations/github/default-rules.js +129 -73
  91. package/dist/integrations/github/default-rules.js.map +1 -1
  92. package/dist/integrations/github/issue-reconciler.d.ts.map +1 -1
  93. package/dist/integrations/github/issue-reconciler.js +3 -1
  94. package/dist/integrations/github/issue-reconciler.js.map +1 -1
  95. package/dist/integrations/github/pull-request-source.d.ts +8 -0
  96. package/dist/integrations/github/pull-request-source.d.ts.map +1 -0
  97. package/dist/integrations/github/pull-request-source.js +17 -0
  98. package/dist/integrations/github/pull-request-source.js.map +1 -0
  99. package/dist/integrations/github/review-status-reporter.d.ts.map +1 -1
  100. package/dist/integrations/github/review-status-reporter.js +7 -3
  101. package/dist/integrations/github/review-status-reporter.js.map +1 -1
  102. package/dist/integrations/github/routes.js +1 -1
  103. package/dist/integrations/github/rules.d.ts +12 -7
  104. package/dist/integrations/github/rules.d.ts.map +1 -1
  105. package/dist/integrations/github/rules.js +214 -93
  106. package/dist/integrations/github/rules.js.map +1 -1
  107. package/dist/integrations/github/session-subscriptions.js +2 -2
  108. package/dist/integrations/github/session-subscriptions.js.map +1 -1
  109. package/dist/integrations/github/token-refresh.d.ts +2 -1
  110. package/dist/integrations/github/token-refresh.d.ts.map +1 -1
  111. package/dist/integrations/github/token-refresh.js.map +1 -1
  112. package/dist/integrations/gitlab/agent-tools.js +1 -1
  113. package/dist/integrations/gitlab/default-rules.d.ts +73 -31
  114. package/dist/integrations/gitlab/default-rules.d.ts.map +1 -1
  115. package/dist/integrations/gitlab/default-rules.js +106 -43
  116. package/dist/integrations/gitlab/default-rules.js.map +1 -1
  117. package/dist/integrations/gitlab/issue-reconciler.d.ts.map +1 -1
  118. package/dist/integrations/gitlab/issue-reconciler.js +2 -1
  119. package/dist/integrations/gitlab/issue-reconciler.js.map +1 -1
  120. package/dist/integrations/gitlab/merge-request-reconciler.d.ts.map +1 -1
  121. package/dist/integrations/gitlab/merge-request-reconciler.js +11 -4
  122. package/dist/integrations/gitlab/merge-request-reconciler.js.map +1 -1
  123. package/dist/integrations/gitlab/routes.d.ts.map +1 -1
  124. package/dist/integrations/gitlab/routes.js +1 -3
  125. package/dist/integrations/gitlab/routes.js.map +1 -1
  126. package/dist/integrations/gitlab/rules.d.ts +6 -1
  127. package/dist/integrations/gitlab/rules.d.ts.map +1 -1
  128. package/dist/integrations/gitlab/rules.js +115 -27
  129. package/dist/integrations/gitlab/rules.js.map +1 -1
  130. package/dist/integrations/gitlab/session-subscriptions.js +1 -1
  131. package/dist/integrations/incidentio/agent-tools.js +6 -6
  132. package/dist/integrations/incidentio/agent-tools.js.map +1 -1
  133. package/dist/integrations/incidentio/default-rules.d.ts.map +1 -1
  134. package/dist/integrations/incidentio/default-rules.js +3 -3
  135. package/dist/integrations/incidentio/default-rules.js.map +1 -1
  136. package/dist/integrations/incidentio/issue-reconciler.d.ts.map +1 -1
  137. package/dist/integrations/incidentio/issue-reconciler.js +2 -1
  138. package/dist/integrations/incidentio/issue-reconciler.js.map +1 -1
  139. package/dist/integrations/incidentio/routes.d.ts +2 -2
  140. package/dist/integrations/incidentio/routes.d.ts.map +1 -1
  141. package/dist/integrations/incidentio/routes.js +10 -13
  142. package/dist/integrations/incidentio/routes.js.map +1 -1
  143. package/dist/integrations/incidentio/rules.d.ts +4 -4
  144. package/dist/integrations/incidentio/rules.d.ts.map +1 -1
  145. package/dist/integrations/incidentio/rules.js +13 -12
  146. package/dist/integrations/incidentio/rules.js.map +1 -1
  147. package/dist/integrations/jira/agent-tools.js +1 -1
  148. package/dist/integrations/jira/binding-scope.d.ts +5 -6
  149. package/dist/integrations/jira/binding-scope.d.ts.map +1 -1
  150. package/dist/integrations/jira/binding-scope.js +9 -11
  151. package/dist/integrations/jira/binding-scope.js.map +1 -1
  152. package/dist/integrations/jira/default-rules.d.ts.map +1 -1
  153. package/dist/integrations/jira/default-rules.js +3 -3
  154. package/dist/integrations/jira/default-rules.js.map +1 -1
  155. package/dist/integrations/jira/issue-reconciler.d.ts.map +1 -1
  156. package/dist/integrations/jira/issue-reconciler.js +2 -1
  157. package/dist/integrations/jira/issue-reconciler.js.map +1 -1
  158. package/dist/integrations/jira/routes.d.ts.map +1 -1
  159. package/dist/integrations/jira/routes.js +3 -5
  160. package/dist/integrations/jira/routes.js.map +1 -1
  161. package/dist/integrations/jira/rules.d.ts +4 -4
  162. package/dist/integrations/jira/rules.d.ts.map +1 -1
  163. package/dist/integrations/jira/rules.js +13 -12
  164. package/dist/integrations/jira/rules.js.map +1 -1
  165. package/dist/integrations/linear/agent-tools.d.ts +9 -1
  166. package/dist/integrations/linear/agent-tools.d.ts.map +1 -1
  167. package/dist/integrations/linear/agent-tools.js +80 -2
  168. package/dist/integrations/linear/agent-tools.js.map +1 -1
  169. package/dist/integrations/linear/default-rules.d.ts.map +1 -1
  170. package/dist/integrations/linear/default-rules.js +3 -3
  171. package/dist/integrations/linear/default-rules.js.map +1 -1
  172. package/dist/integrations/linear/integration.d.ts +4 -1
  173. package/dist/integrations/linear/integration.d.ts.map +1 -1
  174. package/dist/integrations/linear/integration.js +21 -1
  175. package/dist/integrations/linear/integration.js.map +1 -1
  176. package/dist/integrations/linear/issue-reconciler.d.ts.map +1 -1
  177. package/dist/integrations/linear/issue-reconciler.js +2 -1
  178. package/dist/integrations/linear/issue-reconciler.js.map +1 -1
  179. package/dist/integrations/linear/routes.d.ts +2 -1
  180. package/dist/integrations/linear/routes.d.ts.map +1 -1
  181. package/dist/integrations/linear/routes.js +15 -17
  182. package/dist/integrations/linear/routes.js.map +1 -1
  183. package/dist/integrations/linear/rules.d.ts +4 -4
  184. package/dist/integrations/linear/rules.d.ts.map +1 -1
  185. package/dist/integrations/linear/rules.js +14 -13
  186. package/dist/integrations/linear/rules.js.map +1 -1
  187. package/dist/integrations/platform/connect/session-tools.d.ts +5 -0
  188. package/dist/integrations/platform/connect/session-tools.d.ts.map +1 -1
  189. package/dist/integrations/platform/connect/session-tools.js +5 -0
  190. package/dist/integrations/platform/connect/session-tools.js.map +1 -1
  191. package/dist/integrations/platform/github/integration.d.ts.map +1 -1
  192. package/dist/integrations/platform/github/integration.js +33 -21
  193. package/dist/integrations/platform/github/integration.js.map +1 -1
  194. package/dist/integrations/platform/jira/agent-tools.js +1 -1
  195. package/dist/integrations/platform/jira/routes.d.ts.map +1 -1
  196. package/dist/integrations/platform/jira/routes.js +3 -5
  197. package/dist/integrations/platform/jira/routes.js.map +1 -1
  198. package/dist/integrations/platform/linear/integration.d.ts +1 -1
  199. package/dist/integrations/platform/linear/integration.d.ts.map +1 -1
  200. package/dist/integrations/platform/linear/integration.js +6 -5
  201. package/dist/integrations/platform/linear/integration.js.map +1 -1
  202. package/dist/integrations/slack/chat-tools.js +1 -1
  203. package/dist/integrations/slack/reply-tool.js +1 -1
  204. package/dist/integrations/slack/share-file-tool.js +1 -1
  205. package/dist/integrations/slack/slack.d.ts +10 -4
  206. package/dist/integrations/slack/slack.d.ts.map +1 -1
  207. package/dist/integrations/slack/slack.js +19 -9
  208. package/dist/integrations/slack/slack.js.map +1 -1
  209. package/dist/links.d.ts.map +1 -1
  210. package/dist/links.js +3 -2
  211. package/dist/links.js.map +1 -1
  212. package/dist/mcp/routes.js +2 -2
  213. package/dist/routes/attention-parked.d.ts +1 -0
  214. package/dist/routes/attention-parked.d.ts.map +1 -1
  215. package/dist/routes/attention-parked.js +1 -1
  216. package/dist/routes/attention-parked.js.map +1 -1
  217. package/dist/routes/automation-runs.d.ts +5 -1
  218. package/dist/routes/automation-runs.d.ts.map +1 -1
  219. package/dist/routes/automation-runs.js +15 -0
  220. package/dist/routes/automation-runs.js.map +1 -1
  221. package/dist/routes/boards.d.ts +1 -1
  222. package/dist/routes/boards.d.ts.map +1 -1
  223. package/dist/routes/boards.js +1 -17
  224. package/dist/routes/boards.js.map +1 -1
  225. package/dist/routes/config.d.ts +4 -0
  226. package/dist/routes/config.d.ts.map +1 -1
  227. package/dist/routes/config.js +15 -0
  228. package/dist/routes/config.js.map +1 -1
  229. package/dist/routes/contracts.d.ts +128 -24
  230. package/dist/routes/contracts.d.ts.map +1 -1
  231. package/dist/routes/contracts.js +45 -24
  232. package/dist/routes/contracts.js.map +1 -1
  233. package/dist/routes/fs.d.ts +4 -0
  234. package/dist/routes/fs.d.ts.map +1 -1
  235. package/dist/routes/fs.js +8 -4
  236. package/dist/routes/fs.js.map +1 -1
  237. package/dist/routes/intake-routes.d.ts.map +1 -1
  238. package/dist/routes/intake-routes.js +6 -1
  239. package/dist/routes/intake-routes.js.map +1 -1
  240. package/dist/routes/intake.d.ts +0 -23
  241. package/dist/routes/intake.d.ts.map +1 -1
  242. package/dist/routes/intake.js +13 -309
  243. package/dist/routes/intake.js.map +1 -1
  244. package/dist/routes/projects.d.ts.map +1 -1
  245. package/dist/routes/projects.js +9 -3
  246. package/dist/routes/projects.js.map +1 -1
  247. package/dist/routes/provider-credentials.d.ts.map +1 -1
  248. package/dist/routes/provider-credentials.js +10 -5
  249. package/dist/routes/provider-credentials.js.map +1 -1
  250. package/dist/routes/running-packages.d.ts +13 -0
  251. package/dist/routes/running-packages.d.ts.map +1 -0
  252. package/dist/routes/running-packages.js +45 -0
  253. package/dist/routes/running-packages.js.map +1 -0
  254. package/dist/routes/surface.d.ts +10 -1
  255. package/dist/routes/surface.d.ts.map +1 -1
  256. package/dist/routes/surface.js +104 -37
  257. package/dist/routes/surface.js.map +1 -1
  258. package/dist/routes/work-items.d.ts.map +1 -1
  259. package/dist/routes/work-items.js +23 -3
  260. package/dist/routes/work-items.js.map +1 -1
  261. package/dist/rules/binding-context.d.ts.map +1 -1
  262. package/dist/rules/binding-context.js +1 -0
  263. package/dist/rules/binding-context.js.map +1 -1
  264. package/dist/rules/dispatcher.d.ts +0 -6
  265. package/dist/rules/dispatcher.d.ts.map +1 -1
  266. package/dist/rules/dispatcher.js +354 -60
  267. package/dist/rules/dispatcher.js.map +1 -1
  268. package/dist/rules/event-rules.d.ts +33 -2
  269. package/dist/rules/event-rules.d.ts.map +1 -1
  270. package/dist/rules/event-rules.js +46 -11
  271. package/dist/rules/event-rules.js.map +1 -1
  272. package/dist/rules/index.d.ts +2 -4
  273. package/dist/rules/index.d.ts.map +1 -1
  274. package/dist/rules/index.js +2 -3
  275. package/dist/rules/phase-goal.d.ts +54 -0
  276. package/dist/rules/phase-goal.d.ts.map +1 -0
  277. package/dist/rules/phase-goal.js +70 -0
  278. package/dist/rules/phase-goal.js.map +1 -0
  279. package/dist/rules/phase-tools.d.ts +11 -0
  280. package/dist/rules/phase-tools.d.ts.map +1 -0
  281. package/dist/rules/phase-tools.js +19 -0
  282. package/dist/rules/phase-tools.js.map +1 -0
  283. package/dist/rules/processor.d.ts.map +1 -1
  284. package/dist/rules/processor.js +112 -11
  285. package/dist/rules/processor.js.map +1 -1
  286. package/dist/rules/review-source-tool.d.ts +4 -1
  287. package/dist/rules/review-source-tool.d.ts.map +1 -1
  288. package/dist/rules/review-source-tool.js +24 -4
  289. package/dist/rules/review-source-tool.js.map +1 -1
  290. package/dist/rules/rule-group.d.ts +1 -1
  291. package/dist/rules/rule-group.d.ts.map +1 -1
  292. package/dist/rules/rule-group.js +15 -6
  293. package/dist/rules/rule-group.js.map +1 -1
  294. package/dist/rules/start-coordinator.d.ts +5 -1
  295. package/dist/rules/start-coordinator.d.ts.map +1 -1
  296. package/dist/rules/start-coordinator.js +9 -3
  297. package/dist/rules/start-coordinator.js.map +1 -1
  298. package/dist/rules/tools.d.ts.map +1 -1
  299. package/dist/rules/tools.js +39 -28
  300. package/dist/rules/tools.js.map +1 -1
  301. package/dist/rules/transition-service.d.ts +4 -10
  302. package/dist/rules/transition-service.d.ts.map +1 -1
  303. package/dist/rules/transition-service.js +61 -55
  304. package/dist/rules/transition-service.js.map +1 -1
  305. package/dist/rules/types.d.ts +86 -36
  306. package/dist/rules/types.d.ts.map +1 -1
  307. package/dist/rules/types.js +5 -41
  308. package/dist/rules/types.js.map +1 -1
  309. package/dist/rules/validation.d.ts.map +1 -1
  310. package/dist/rules/validation.js +32 -2
  311. package/dist/rules/validation.js.map +1 -1
  312. package/dist/secret-encryption.d.ts +21 -0
  313. package/dist/secret-encryption.d.ts.map +1 -1
  314. package/dist/secret-encryption.js +31 -4
  315. package/dist/secret-encryption.js.map +1 -1
  316. package/dist/session/self-review-gate.d.ts +6 -4
  317. package/dist/session/self-review-gate.d.ts.map +1 -1
  318. package/dist/session/self-review-gate.js +9 -12
  319. package/dist/session/self-review-gate.js.map +1 -1
  320. package/dist/session/session-threads.d.ts +2 -3
  321. package/dist/session/session-threads.d.ts.map +1 -1
  322. package/dist/session/session-threads.js.map +1 -1
  323. package/dist/session/source-control-tools.d.ts +39 -13
  324. package/dist/session/source-control-tools.d.ts.map +1 -1
  325. package/dist/session/source-control-tools.js +55 -61
  326. package/dist/session/source-control-tools.js.map +1 -1
  327. package/dist/session/work-item-state-processor.d.ts +24 -0
  328. package/dist/session/work-item-state-processor.d.ts.map +1 -0
  329. package/dist/session/work-item-state-processor.js +275 -0
  330. package/dist/session/work-item-state-processor.js.map +1 -0
  331. package/dist/storage/domains/boards/base.d.ts +70 -31
  332. package/dist/storage/domains/boards/base.d.ts.map +1 -1
  333. package/dist/storage/domains/boards/base.js +99 -126
  334. package/dist/storage/domains/boards/base.js.map +1 -1
  335. package/dist/storage/domains/boards/schemas.d.ts.map +1 -1
  336. package/dist/storage/domains/boards/schemas.js +12 -0
  337. package/dist/storage/domains/boards/schemas.js.map +1 -1
  338. package/dist/storage/domains/intake/base.d.ts +5 -45
  339. package/dist/storage/domains/intake/base.d.ts.map +1 -1
  340. package/dist/storage/domains/intake/base.js +11 -89
  341. package/dist/storage/domains/intake/base.js.map +1 -1
  342. package/dist/storage/domains/projects/base.d.ts +17 -9
  343. package/dist/storage/domains/projects/base.d.ts.map +1 -1
  344. package/dist/storage/domains/projects/base.js +15 -7
  345. package/dist/storage/domains/projects/base.js.map +1 -1
  346. package/dist/storage/domains/work-items/base.d.ts +39 -4
  347. package/dist/storage/domains/work-items/base.d.ts.map +1 -1
  348. package/dist/storage/domains/work-items/base.js +113 -26
  349. package/dist/storage/domains/work-items/base.js.map +1 -1
  350. package/dist/storage/domains/work-items/cards.d.ts +24 -2
  351. package/dist/storage/domains/work-items/cards.d.ts.map +1 -1
  352. package/dist/storage/domains/work-items/cards.js +118 -8
  353. package/dist/storage/domains/work-items/cards.js.map +1 -1
  354. package/dist/storage/domains/work-items/legacy-review.d.ts +63 -0
  355. package/dist/storage/domains/work-items/legacy-review.d.ts.map +1 -0
  356. package/dist/storage/domains/work-items/legacy-review.js +95 -0
  357. package/dist/storage/domains/work-items/legacy-review.js.map +1 -0
  358. package/dist/storage/domains/work-items/metrics.d.ts +12 -4
  359. package/dist/storage/domains/work-items/metrics.d.ts.map +1 -1
  360. package/dist/storage/domains/work-items/metrics.js +14 -32
  361. package/dist/storage/domains/work-items/metrics.js.map +1 -1
  362. package/dist/storage/domains/work-items/work-item-schema.d.ts +2 -0
  363. package/dist/storage/domains/work-items/work-item-schema.d.ts.map +1 -1
  364. package/dist/storage/domains/work-items/work-item-schema.js +5 -0
  365. package/dist/storage/domains/work-items/work-item-schema.js.map +1 -1
  366. package/dist/tools/approval-tools.d.ts.map +1 -1
  367. package/dist/tools/approval-tools.js +28 -9
  368. package/dist/tools/approval-tools.js.map +1 -1
  369. package/dist/tools/index.d.ts +1 -1
  370. package/dist/tools/index.js +1 -1
  371. package/dist/tools/index.js.map +1 -1
  372. package/dist/tools/messaging-tools.d.ts +3 -0
  373. package/dist/tools/messaging-tools.d.ts.map +1 -1
  374. package/dist/tools/messaging-tools.js +2 -1
  375. package/dist/tools/messaging-tools.js.map +1 -1
  376. package/dist/tools/read-tools.d.ts +8 -2
  377. package/dist/tools/read-tools.d.ts.map +1 -1
  378. package/dist/tools/read-tools.js +83 -23
  379. package/dist/tools/read-tools.js.map +1 -1
  380. package/dist/tools/reports.d.ts +1 -0
  381. package/dist/tools/reports.d.ts.map +1 -1
  382. package/dist/tools/reports.js +2 -1
  383. package/dist/tools/reports.js.map +1 -1
  384. package/dist/tools/run-watch.d.ts +1 -1
  385. package/dist/tools/run-watch.d.ts.map +1 -1
  386. package/dist/tools/run-watch.js +1 -0
  387. package/dist/tools/run-watch.js.map +1 -1
  388. package/dist/tools/sandbox-tools.js +1 -1
  389. package/dist/tools/session-tools.js +1 -1
  390. package/dist/tools/sessions-state-processor.d.ts +2 -0
  391. package/dist/tools/sessions-state-processor.d.ts.map +1 -1
  392. package/dist/tools/sessions-state-processor.js +4 -2
  393. package/dist/tools/sessions-state-processor.js.map +1 -1
  394. package/dist/tools/side-sessions.d.ts +2 -0
  395. package/dist/tools/side-sessions.d.ts.map +1 -1
  396. package/dist/tools/side-sessions.js +2 -1
  397. package/dist/tools/side-sessions.js.map +1 -1
  398. package/dist/tools/work-item-tools.js +3 -3
  399. package/dist/tools/work-item-tools.js.map +1 -1
  400. package/dist/work-item-branch.d.ts +13 -0
  401. package/dist/work-item-branch.d.ts.map +1 -1
  402. package/dist/work-item-branch.js +16 -1
  403. package/dist/work-item-branch.js.map +1 -1
  404. package/dist/work-items/create-work-item.d.ts +6 -0
  405. package/dist/work-items/create-work-item.d.ts.map +1 -1
  406. package/dist/work-items/create-work-item.js +25 -1
  407. package/dist/work-items/create-work-item.js.map +1 -1
  408. package/dist/work-items/providers/base.d.ts +4 -1
  409. package/dist/work-items/providers/base.d.ts.map +1 -1
  410. package/dist/work-items/providers/base.js +6 -1
  411. package/dist/work-items/providers/base.js.map +1 -1
  412. package/dist/work-items/providers/github.d.ts.map +1 -1
  413. package/dist/work-items/providers/github.js +2 -1
  414. package/dist/work-items/providers/github.js.map +1 -1
  415. package/dist/work-items/providers/gitlab.d.ts.map +1 -1
  416. package/dist/work-items/providers/gitlab.js +2 -1
  417. package/dist/work-items/providers/gitlab.js.map +1 -1
  418. package/dist/work-items/providers/jira.d.ts.map +1 -1
  419. package/dist/work-items/providers/jira.js +3 -2
  420. package/dist/work-items/providers/jira.js.map +1 -1
  421. package/dist/work-items/providers/linear.d.ts.map +1 -1
  422. package/dist/work-items/providers/linear.js +3 -2
  423. package/dist/work-items/providers/linear.js.map +1 -1
  424. package/dist/work-items/providers/types.d.ts +6 -0
  425. package/dist/work-items/providers/types.d.ts.map +1 -1
  426. package/dist/work-items/split-migration.d.ts.map +1 -1
  427. package/dist/work-items/split-migration.js +1 -0
  428. package/dist/work-items/split-migration.js.map +1 -1
  429. package/dist/workspace.d.ts.map +1 -1
  430. package/dist/workspace.js +53 -10
  431. package/dist/workspace.js.map +1 -1
  432. package/factory-skills/configure-factory-rules/SKILL.md +12 -10
  433. package/factory-skills/factory-build/SKILL.md +64 -0
  434. package/factory-skills/factory-gitlab-rereview/SKILL.md +2 -2
  435. package/factory-skills/factory-gitlab-review/SKILL.md +3 -3
  436. package/factory-skills/factory-plan/SKILL.md +8 -10
  437. package/factory-skills/factory-rereview/SKILL.md +5 -5
  438. package/factory-skills/factory-review/SKILL.md +16 -6
  439. package/factory-skills/factory-self-review/SKILL.md +31 -11
  440. package/factory-skills/factory-triage/SKILL.md +25 -10
  441. package/package.json +11 -11
  442. package/dist/boards/index.js.map +0 -1
  443. package/dist/boards/pull-request-review.d.ts +0 -14
  444. package/dist/boards/pull-request-review.d.ts.map +0 -1
  445. package/dist/boards/pull-request-review.js +0 -81
  446. package/dist/boards/pull-request-review.js.map +0 -1
  447. package/dist/boards/work-tool-rules.d.ts +0 -8
  448. package/dist/boards/work-tool-rules.d.ts.map +0 -1
  449. package/dist/boards/work-tool-rules.js +0 -19
  450. package/dist/boards/work-tool-rules.js.map +0 -1
  451. package/dist/rules/resolve.d.ts +0 -15
  452. package/dist/rules/resolve.d.ts.map +0 -1
  453. package/dist/rules/resolve.js +0 -22
  454. package/dist/rules/resolve.js.map +0 -1
  455. package/factory-skills/factory-complete-issue/SKILL.md +0 -24
@@ -1,15 +1,17 @@
1
1
  import { closeReason } from "../../boards/board-events.js";
2
2
  import "../../boards/index.js";
3
- import { boardEventHandlers, boardForWorkItem, eventBoardId, rulePhaseField, withBoardClosure, workItemPhaseSemantics } from "../../boards/semantics.js";
4
- import { placedByIntakeRoute } from "../../boards/intake-routing.js";
5
- import { cardLabels, moveCardToBoard } from "../../boards/relocate.js";
3
+ import { toBoardSource } from "../../boards/project-boards.js";
4
+ import { cardLabels } from "../../boards/relocate.js";
5
+ import { boardEventHandlers, boardForWorkItem, cardBoardFlow, cardOwnsBranch, eventBoardId, intakeTarget, isTerminalWorkItem, newestOwnedOpenPullRequestNumber, pullRequestSources, rulePhaseField, withBoardClosure, withBoardFlow, workItemPhaseSemantics } from "../../boards/semantics.js";
6
6
  import { requestsChangesVerdict } from "../../review-verdict.js";
7
7
  import { deploymentOwnsEvent, routeEventDecision, selectEventRule, withoutRouteEvent } from "../../rules/event-rules.js";
8
8
  import { assertFactoryDecisionTarget, validateFactoryRuleDecisions } from "../../rules/validation.js";
9
- import { resolveIntakeLabelRoute } from "../../storage/domains/intake/base.js";
10
9
  import { FACTORY_PULL_REQUEST_RECONCILIATION_KEY, WorkItemUpdateConflictError } from "../../storage/domains/work-items/base.js";
11
10
  import { primarySource } from "../../storage/domains/work-items/work-item-schema.js";
11
+ import { workItemBranch, workItemBranchSource } from "../../work-item-branch.js";
12
+ import { closingIssueNumbers } from "../closing-references.js";
12
13
  import { recordPullRequestHead } from "../pull-request-head.js";
14
+ import { isRepositoryPullRequest } from "./pull-request-source.js";
13
15
  import { changeRequestTargetKey } from "./subscriptions.js";
14
16
  import { createHash } from "crypto";
15
17
  // src/integrations/github/rules.ts
@@ -162,6 +164,36 @@ function canonicalSourceKey(kind, itemNumber) {
162
164
  function legacySourceKey(repositoryId, kind, itemNumber) {
163
165
  return `github:${repositoryId}:${kind}:${itemNumber}`;
164
166
  }
167
+ /**
168
+ * The head branch of a pull request pushed to the repository itself. A fork's branch lives in
169
+ * another repository, so its name says nothing about which card wrote it — anyone could name a
170
+ * fork branch after a card. A payload without `head.repo` (rebuilt from polled state) is the
171
+ * base repository's own record and keeps its branch.
172
+ */
173
+ function sameRepositoryHeadBranch(pullRequest, repositoryId) {
174
+ const head = object(pullRequest?.head);
175
+ if (head && "repo" in head && number(object(head.repo)?.id) !== repositoryId) return void 0;
176
+ return string(head?.ref);
177
+ }
178
+ /** A card attributed to a different repository than the pull request's. */
179
+ function cardInOtherRepository(item, repositoryId, repositoryFullName) {
180
+ return (/^https?:\/\/[^/]+\/.+\/(?:issues|pull)\/\d+(?:[/?#]|$)/.test(item.externalSource?.url ?? "") || typeof item.workItem.metadata?.githubRepositoryId === "number") && !cardBelongsToRepository(item, repositoryId, repositoryFullName);
181
+ }
182
+ /**
183
+ * The work item a pull request's head branch belongs to: a session of the item checked it out,
184
+ * or it is the item's own branch name, which covers pull requests opened from a session that
185
+ * only follows the card (no session on the card holds the branch, and no provenance was
186
+ * recorded). Only a single match counts; two cards claiming one branch name neither.
187
+ */
188
+ function branchAuthoringItem(items, headBranch, repositoryId, repositoryFullName) {
189
+ if (!headBranch) return void 0;
190
+ const matches = items.filter((item) => item.externalSource?.type !== "pull-request" && !cardInOtherRepository(item, repositoryId, repositoryFullName) && (Object.values(item.sessions).some((session) => session.branch === headBranch) || workItemBranch({
191
+ id: item.id,
192
+ source: workItemBranchSource(item.externalSource),
193
+ metadata: item.workItem.metadata
194
+ }) === headBranch));
195
+ return matches.length === 1 ? matches[0] : void 0;
196
+ }
165
197
  /** Every source a card for this issue or pull request may be filed under. */
166
198
  function githubItemSources(repositoryId, kind, itemNumber) {
167
199
  return [canonicalSourceKey(kind, itemNumber), legacySourceKey(repositoryId, kind, itemNumber)].map((externalId) => ({
@@ -170,6 +202,14 @@ function githubItemSources(repositoryId, kind, itemNumber) {
170
202
  externalId
171
203
  }));
172
204
  }
205
+ /**
206
+ * The pull request source an issue card carries for this repository's pull request, when it was
207
+ * attached to the card rather than filed on a card of its own.
208
+ */
209
+ function attachedPullRequestSource(item, repositoryId, repositoryFullName, pullRequestNumber) {
210
+ if (item.externalSource?.type === "pull-request") return void 0;
211
+ return pullRequestSources(item).find((source) => isRepositoryPullRequest(source, repositoryId, repositoryFullName, pullRequestNumber));
212
+ }
173
213
  function provenanceTarget(repositoryId, pullRequestNumber) {
174
214
  return `factory-pr-provenance:${repositoryId}:${pullRequestNumber}`;
175
215
  }
@@ -225,8 +265,8 @@ function pullRequestProvenance(data, factoryProjectId) {
225
265
  workItemId: data.workItemId
226
266
  };
227
267
  }
228
- /** Identity under which label-driven relocations are recorded. */
229
- const LABEL_ROUTE_USER_ID = "factory-rule-dispatcher";
268
+ /** Identity under which label and assignee syncs are recorded. */
269
+ const CARD_SYNC_USER_ID = "factory-rule-dispatcher";
230
270
  /**
231
271
  * Whether a login is Factory itself. Prefers the resolved identity, which is
232
272
  * observed from Factory's own writes, and falls back to the configured slug.
@@ -242,6 +282,14 @@ var GithubRules = class {
242
282
  constructor(options) {
243
283
  this.options = options;
244
284
  }
285
+ /** Brings the project's boards up to date, so the sync reads below see its stored boards. */
286
+ async loadBoards(scope) {
287
+ await toBoardSource(this.options.boards).load(scope);
288
+ }
289
+ /** The project's loaded boards; {@link loadBoards} runs before an event is handled. */
290
+ #boards(scope) {
291
+ return toBoardSource(this.options.boards).forProject(scope);
292
+ }
245
293
  /**
246
294
  * Whether a login is Factory itself. Prefers the resolved identity, which is
247
295
  * observed from Factory's own writes, and falls back to the configured slug.
@@ -258,80 +306,31 @@ var GithubRules = class {
258
306
  return slug ? `${slug.toLowerCase()}[bot]` : void 0;
259
307
  }
260
308
  /**
261
- * Board an issue lands on: the one its labels select under the project's label routes, else the
262
- * board the repository's intake source binding names. Only installed boards count. Undefined
263
- * leaves built-in routing (Work) in charge.
309
+ * `labeled` / `unlabeled` are not rule events: the card's label metadata is refreshed and the card
310
+ * stays where it is. Intake routes placed it when it arrived; a label change doesn't move it.
264
311
  */
265
- async #intakeTarget(project, repositoryName, labels) {
266
- const intake = this.options.intake;
267
- if (!intake) return void 0;
268
- const { orgId, factoryProjectId } = project;
269
- if (labels.length > 0) {
270
- const routes = await intake.listLabelRoutes({
271
- orgId,
272
- factoryProjectId,
273
- integrationId: "github"
274
- });
275
- const route = resolveIntakeLabelRoute(routes, labels);
276
- const board = route ? this.options.boards.get(route.board) : void 0;
277
- if (board) return {
278
- board: board.id,
279
- initialPhase: board.initialPhase
280
- };
281
- }
282
- const slug = repositoryName.toLowerCase();
283
- const binding = (await intake.listBindings({
284
- orgId,
285
- integrationId: "github"
286
- })).find((candidate) => candidate.factoryProjectId === factoryProjectId && (candidate.sourceId === project.projectRepository.repositoryId || candidate.sourceId.toLowerCase() === slug));
287
- const board = binding?.board ? this.options.boards.get(binding.board) : void 0;
288
- return board ? {
289
- board: board.id,
290
- initialPhase: board.initialPhase
291
- } : void 0;
292
- }
293
- /**
294
- * `labeled` / `unlabeled` are not rule events: the labels only decide which board the card belongs
295
- * on, so the card is re-routed here and its label metadata refreshed. Cards a run owns, and
296
- * finished cards, stay where they are.
297
- */
298
- async #relocateLabeledIssue(parsed, repositoryId, repositoryName, project) {
312
+ async #syncIssueLabels(parsed, repositoryId, repositoryName, project) {
299
313
  const issue = object(parsed.payload.issue);
300
314
  const issueNumber = issue?.pull_request === void 0 ? number(issue?.number) : void 0;
301
315
  if (!issueNumber) return { status: "ignored" };
302
316
  const found = await this.#relatedItem(project.orgId, project.factoryProjectId, repositoryId, repositoryName, issueNumber, void 0, void 0, null);
303
317
  if (!found || found.externalSource?.type !== "issue") return { status: "ignored" };
304
318
  const labels = labelNames(issue?.labels);
305
- let item = found;
306
- let changed = false;
307
- if (!sameLabels(cardLabels(item), labels)) {
308
- let updated;
309
- try {
310
- updated = await this.options.storage.syncCard({
311
- orgId: item.orgId,
312
- id: item.id,
313
- userId: LABEL_ROUTE_USER_ID,
314
- patch: { metadata: { labels } },
315
- expectedSourceRevision: primarySource(item)?.revision
316
- });
317
- } catch (error) {
318
- if (!(error instanceof WorkItemUpdateConflictError)) throw error;
319
- return { status: "ignored" };
320
- }
321
- if (!updated) return { status: "ignored" };
322
- item = updated.item;
323
- changed = true;
319
+ if (sameLabels(cardLabels(found), labels)) return { status: "ignored" };
320
+ let updated;
321
+ try {
322
+ updated = await this.options.storage.syncCard({
323
+ orgId: found.orgId,
324
+ id: found.id,
325
+ userId: CARD_SYNC_USER_ID,
326
+ patch: { metadata: { labels } },
327
+ expectedSourceRevision: primarySource(found)?.revision
328
+ });
329
+ } catch (error) {
330
+ if (!(error instanceof WorkItemUpdateConflictError)) throw error;
331
+ return { status: "ignored" };
324
332
  }
325
- if (placedByIntakeRoute(item)) return { status: changed ? "committed" : "ignored" };
326
- const target = await this.#intakeTarget(project, repositoryName, labels);
327
- const outcome = await moveCardToBoard({
328
- workItems: this.options.storage,
329
- boardRegistry: this.options.boards,
330
- userId: LABEL_ROUTE_USER_ID,
331
- item,
332
- targetBoard: target?.board ?? "work"
333
- });
334
- return { status: changed || outcome === "moved" ? "committed" : "ignored" };
333
+ return { status: updated ? "committed" : "ignored" };
335
334
  }
336
335
  /**
337
336
  * `assigned` / `unassigned` are not rule events either: an existing issue card only mirrors the
@@ -354,7 +353,7 @@ var GithubRules = class {
354
353
  return { status: await this.options.storage.syncCard({
355
354
  orgId: item.orgId,
356
355
  id: item.id,
357
- userId: LABEL_ROUTE_USER_ID,
356
+ userId: CARD_SYNC_USER_ID,
358
357
  patch: { metadata: {
359
358
  assignees,
360
359
  ...payloadTime !== void 0 ? { githubAssigneesUpdatedAt: updatedAt } : {}
@@ -371,7 +370,7 @@ var GithubRules = class {
371
370
  if (parsed.event !== "issues") return void 0;
372
371
  switch (string(parsed.payload.action)) {
373
372
  case "labeled":
374
- case "unlabeled": return (...args) => this.#relocateLabeledIssue(...args);
373
+ case "unlabeled": return (...args) => this.#syncIssueLabels(...args);
375
374
  case "assigned":
376
375
  case "unassigned": return (...args) => this.#syncIssueAssignees(...args);
377
376
  default: return;
@@ -398,7 +397,10 @@ var GithubRules = class {
398
397
  });
399
398
  if (projects.length === 0) return { status: "ignored" };
400
399
  const results = [];
401
- for (const project of projects) results.push(await handle(project));
400
+ for (const project of projects) {
401
+ await this.loadBoards(project);
402
+ results.push(await handle(project));
403
+ }
402
404
  if (results.some((result) => result.status === "committed")) return { status: "committed" };
403
405
  if (results.some((result) => result.status === "replayed")) return { status: "replayed" };
404
406
  return results[0] ?? { status: "ignored" };
@@ -450,9 +452,9 @@ var GithubRules = class {
450
452
  const requestedReviewer = string(object(parsed.payload.requested_reviewer)?.login);
451
453
  const pullRequestAuthor = string(object(pullRequest?.user)?.login);
452
454
  const pullRequestFactoryAuthored = provenance !== null || this.#isFactoryLogin(pullRequestAuthor);
453
- const resolvedItem = await this.#relatedItem(project.orgId, project.factoryProjectId, repositoryId, repositoryName, issueNumber, pullRequestNumber, string(object(pullRequest?.head)?.ref), reReviewEvent ? null : provenance, senderIsResponder && !reviewEntryRequested);
454
- const relatedItem = reviewEntryRequested && resolvedItem?.externalSource?.type !== "pull-request" ? void 0 : resolvedItem;
455
- const intake = issueNumber ? await this.#intakeTarget(project, repositoryName, labelNames(issue?.labels)) : void 0;
455
+ const resolvedItem = await this.#relatedItem(project.orgId, project.factoryProjectId, repositoryId, repositoryName, issueNumber, pullRequestNumber, sameRepositoryHeadBranch(pullRequest, repositoryId), reReviewEvent ? null : provenance, senderIsResponder && !reviewEntryRequested);
456
+ const matchedItem = reviewEntryRequested && resolvedItem?.externalSource?.type !== "pull-request" && !(resolvedItem && pullRequestNumber && attachedPullRequestSource(resolvedItem, repositoryId, repositoryName, pullRequestNumber)) ? void 0 : resolvedItem;
457
+ const intake = intakeTarget(this.#boards(project));
456
458
  const actor = await githubActor(this.options.github, {
457
459
  installationId,
458
460
  repository: repositoryName,
@@ -460,6 +462,22 @@ var GithubRules = class {
460
462
  factoryAuthored: !senderIsResponder && provenance !== null || this.#isFactoryLogin(login)
461
463
  });
462
464
  if (actor.type === "github" && actor.factoryAuthored && (event === "issueCommentCreated" || event === "issueCommentEdited") && string(issueComment?.body)?.includes(FACTORY_TRIAGE_COMMENT_MARKER)) return { status: "ignored" };
465
+ const home = event === "pullRequestOpened" && pullRequestNumber ? await this.#pullRequestHome({
466
+ orgId: project.orgId,
467
+ factoryProjectId: project.factoryProjectId,
468
+ repositoryId,
469
+ repositoryName,
470
+ pullRequestNumber,
471
+ matchedItem,
472
+ factoryAuthored: pullRequestFactoryAuthored,
473
+ authorTrusted: actor.type === "github" && actor.trusted && login === pullRequestAuthor,
474
+ body: string(pullRequest?.body)
475
+ }) : void 0;
476
+ const relatedItem = (home?.reason === "authored" ? home.card : void 0) ?? matchedItem;
477
+ const pullRequestHome = home ? {
478
+ workItemId: home.card.id,
479
+ reason: home.reason
480
+ } : void 0;
463
481
  const evaluate = async (item, ingressIdentity, pullRequestIntake = false, attempt = 1) => {
464
482
  const context = {
465
483
  tenant: {
@@ -489,11 +507,13 @@ var GithubRules = class {
489
507
  },
490
508
  board: boardForWorkItem(item),
491
509
  itemRevision: item.revision,
492
- ...withBoardClosure(this.options.boards, item),
493
- ...rulePhaseField(this.options.boards, item)
510
+ ...withBoardClosure(this.#boards(project), item),
511
+ ...rulePhaseField(this.#boards(project), item),
512
+ ...withBoardFlow(this.#boards(project), item)
494
513
  } : {},
495
514
  ...intake ? { intake } : {},
496
515
  ...pullRequestIntake ? { pullRequestIntake: true } : {},
516
+ ...pullRequestHome ? { pullRequestHome } : {},
497
517
  event,
498
518
  deliveryId: parsed.deliveryId,
499
519
  factory: { createdAt: factoryProject.createdAt.toISOString() },
@@ -541,6 +561,7 @@ var GithubRules = class {
541
561
  ...pullRequestAuthor ? { author: pullRequestAuthor } : {},
542
562
  factoryAuthored: pullRequestFactoryAuthored,
543
563
  headBranch: string(object(pullRequest?.head)?.ref) ?? "",
564
+ ...string(object(pullRequest?.head)?.sha) ? { headSha: string(object(pullRequest?.head)?.sha).toLowerCase() } : {},
544
565
  baseBranch: string(object(pullRequest?.base)?.ref) ?? ""
545
566
  } } : {},
546
567
  ...reviewRequested && requestedReviewer ? { reviewRequest: {
@@ -558,8 +579,8 @@ var GithubRules = class {
558
579
  };
559
580
  const boardEvent = boardEventFor(context);
560
581
  if (boardEvent) context.boardEvent = boardEvent;
561
- const ownedByDeployment = deploymentOwnsEvent(this.options.github.rules, boardEventHandlers(this.options.boards, eventBoardId(context), "github"), event);
562
- const rule = selectEventRule(this.options.github.rules, boardEventHandlers(this.options.boards, eventBoardId(context), "github"), event);
582
+ const ownedByDeployment = deploymentOwnsEvent(this.options.github.rules, boardEventHandlers(this.#boards(project), eventBoardId(context), "github"), event);
583
+ const rule = selectEventRule(this.options.github.rules, boardEventHandlers(this.#boards(project), eventBoardId(context), "github"), event);
563
584
  let decision = void 0;
564
585
  let decisions = [];
565
586
  let outcome = { status: "accepted" };
@@ -571,7 +592,7 @@ var GithubRules = class {
571
592
  reason: decision.reason
572
593
  };
573
594
  else if (decision) decisions = validateFactoryRuleDecisions([decision]).map((entry) => {
574
- assertFactoryDecisionTarget(entry, this.options.boards, item ? boardForWorkItem(item) : void 0);
595
+ assertFactoryDecisionTarget(entry, this.#boards(project), item ? boardForWorkItem(item) : void 0);
575
596
  return { ...entry };
576
597
  });
577
598
  } catch (error) {
@@ -621,6 +642,16 @@ var GithubRules = class {
621
642
  const headSha = string(object(pullRequest?.head)?.sha)?.toLowerCase();
622
643
  if (event !== "pullRequestUpdated" || !headSha || !pullRequestNumber) return;
623
644
  const headAt = timestamp(pullRequest?.updated_at);
645
+ if (relatedItem && attachedPullRequestSource(relatedItem, repositoryId, repositoryName, pullRequestNumber)) {
646
+ await recordPullRequestHead(this.options.storage, {
647
+ orgId: relatedItem.orgId,
648
+ id: relatedItem.id,
649
+ headSha,
650
+ headAt,
651
+ ownedPullRequestNumber: pullRequestNumber
652
+ });
653
+ return;
654
+ }
624
655
  if (relatedItem?.externalSource?.type !== "pull-request") return;
625
656
  const parent = await this.#linkedClosureItem(project.orgId, project.factoryProjectId, repositoryId, repositoryName, pullRequestNumber, relatedItem);
626
657
  await recordPullRequestHead(this.options.storage, {
@@ -645,14 +676,22 @@ var GithubRules = class {
645
676
  const deliveryIdentity = `${installationId}:${parsed.deliveryId}`;
646
677
  const pullRequestOpened = event === "pullRequestOpened";
647
678
  const primary = await evaluate(evaluated, deliveryIdentity, pullRequestOpened);
648
- const linked = (event === "pullRequestMerged" || event === "pullRequestClosed") && relatedItem && pullRequestNumber ? await this.#linkedClosureItem(project.orgId, project.factoryProjectId, repositoryId, repositoryName, pullRequestNumber, relatedItem) : void 0;
679
+ const linked = (event === "pullRequestMerged" || event === "pullRequestClosed") && relatedItem && pullRequestNumber ? await this.#linkedClosureItem(project.orgId, project.factoryProjectId, repositoryId, repositoryName, pullRequestNumber, relatedItem, relatedItem.externalSource?.type === "pull-request" && !relatedItem.parentWorkItemId && (pullRequestFactoryAuthored || pullRequestAuthor !== void 0 && await trustedCollaborator(this.options.github, {
680
+ installationId,
681
+ repository: repositoryName,
682
+ login: pullRequestAuthor
683
+ })) ? sameRepositoryHeadBranch(pullRequest, repositoryId) : void 0) : void 0;
649
684
  const stampClosed = async (companionStatus) => {
650
685
  if (!pullRequestNumber) return;
651
686
  if (pullRequestOpened) {
652
687
  const trusted = actor.type === "github" && actor.trusted || pullRequestFactoryAuthored;
653
688
  if (!authoringItem || !trusted || authoringItem.metadata?.openPullRequestNumber === pullRequestNumber) return;
654
689
  const reviewCard = await this.#linkedClosureItem(project.orgId, project.factoryProjectId, repositoryId, repositoryName, pullRequestNumber, authoringItem);
655
- if (!(companionStatus === "replayed" ? reviewCard !== void 0 && !["closed", "merged"].includes(reviewCard.workItem.state ?? "") : !["closed", "merged"].includes(reviewCard?.workItem.state ?? ""))) return;
690
+ const attached = attachedPullRequestSource(authoringItem, repositoryId, repositoryName, pullRequestNumber);
691
+ const recordedState = reviewCard ? reviewCard.workItem.state : attached?.state;
692
+ if (!(companionStatus === "replayed" ? (reviewCard !== void 0 || attached !== void 0) && !["closed", "merged"].includes(recordedState ?? "") : !["closed", "merged"].includes(recordedState ?? ""))) return;
693
+ const headBranch = string(object(pullRequest?.head)?.ref);
694
+ if (!(home?.reason === "authored" || home === void 0 && (attached === void 0 || pullRequestFactoryAuthored || cardOwnsBranch(authoringItem, headBranch)))) return;
656
695
  await this.options.storage.update({
657
696
  orgId: authoringItem.orgId,
658
697
  id: authoringItem.id,
@@ -669,10 +708,25 @@ var GithubRules = class {
669
708
  for (const card of [relatedItem, linked]) {
670
709
  if (!card) continue;
671
710
  if (card.externalSource?.type !== "pull-request") {
711
+ const attached = attachedPullRequestSource(card, repositoryId, repositoryName, pullRequestNumber);
712
+ if (attached) await this.options.storage.syncWorkItemSource({
713
+ orgId: card.orgId,
714
+ factoryProjectId: card.factoryProjectId,
715
+ provider: attached.provider,
716
+ externalId: attached.externalId,
717
+ patch: {
718
+ state: event === "pullRequestMerged" ? "merged" : "closed",
719
+ metadata: {
720
+ state: "closed",
721
+ merged: event === "pullRequestMerged"
722
+ }
723
+ }
724
+ });
725
+ if (!attached && pullRequestSources(card).length > 0) continue;
672
726
  const recorded = card.metadata?.openPullRequestNumber;
673
727
  const settled = {
674
728
  ...(typeof recorded !== "number" || recorded === pullRequestNumber) && event === "pullRequestMerged" && typeof card.metadata?.reviewVerdict === "string" ? { reviewVerdict: null } : {},
675
- ...recorded === pullRequestNumber ? { openPullRequestNumber: null } : {}
729
+ ...recorded === pullRequestNumber ? { openPullRequestNumber: newestOwnedOpenPullRequestNumber(card, pullRequestNumber) } : {}
676
730
  };
677
731
  if (Object.keys(settled).length > 0) await this.options.storage.update({
678
732
  orgId: card.orgId,
@@ -711,7 +765,7 @@ var GithubRules = class {
711
765
  * heuristics. Returns nothing when the pull request has only one card, which
712
766
  * is every pull request Factory did not open from a work item's session.
713
767
  */
714
- async #linkedClosureItem(orgId, projectId, repositoryId, repositoryFullName, pullRequestNumber, resolved) {
768
+ async #linkedClosureItem(orgId, projectId, repositoryId, repositoryFullName, pullRequestNumber, resolved, headBranch) {
715
769
  await this.options.storage.copyLegacyCards({
716
770
  orgId,
717
771
  factoryProjectId: projectId,
@@ -721,9 +775,42 @@ var GithubRules = class {
721
775
  orgId,
722
776
  factoryProjectId: projectId
723
777
  });
724
- const linked = resolved.externalSource?.type === "pull-request" ? await this.#parentItem(orgId, projectId, items, resolved) : items.find((item) => item.parentWorkItemId === resolved.id && (item.externalSource?.externalId === canonicalSourceKey("pull-request", pullRequestNumber) || item.externalSource?.externalId === legacySourceKey(repositoryId, "pull-request", pullRequestNumber)) && cardBelongsToRepository(item, repositoryId, repositoryFullName));
778
+ const linked = resolved.externalSource?.type === "pull-request" ? resolved.parentWorkItemId ? await this.#parentItem(orgId, projectId, items, resolved) : branchAuthoringItem(items, headBranch, repositoryId, repositoryFullName) : items.find((item) => item.parentWorkItemId === resolved.id && (item.externalSource?.externalId === canonicalSourceKey("pull-request", pullRequestNumber) || item.externalSource?.externalId === legacySourceKey(repositoryId, "pull-request", pullRequestNumber)) && cardBelongsToRepository(item, repositoryId, repositoryFullName));
725
779
  return linked?.id === resolved.id ? void 0 : linked;
726
780
  }
781
+ /**
782
+ * The existing card a pull request opened for the first time belongs with, or undefined when it
783
+ * gets a card of its own. The card must be open, in this repository, and not a pull request's
784
+ * own card. In order: the card that authored it (on a board that reviews pull requests), by provenance or session branch,
785
+ * when the pull request is Factory-authored or its author is trusted; it is filed there. Then,
786
+ * for a trusted author only, the card of the first issue its description says it closes; an
787
+ * intake route decides whether it joins that one. An untrusted author's pull request never
788
+ * lands on another card, whatever it says.
789
+ */
790
+ async #pullRequestHome(input) {
791
+ if (!input.factoryAuthored && !input.authorTrusted) return void 0;
792
+ const items = await this.options.storage.list({
793
+ orgId: input.orgId,
794
+ factoryProjectId: input.factoryProjectId
795
+ });
796
+ if (items.some((item) => attachedPullRequestSource(item, input.repositoryId, input.repositoryName, input.pullRequestNumber) || item.externalSource?.type === "pull-request" && (item.externalSource.externalId === canonicalSourceKey("pull-request", input.pullRequestNumber) || item.externalSource.externalId === legacySourceKey(input.repositoryId, "pull-request", input.pullRequestNumber)) && cardBelongsToRepository(item, input.repositoryId, input.repositoryName))) return void 0;
797
+ const eligible = (card) => card.externalSource?.type !== "pull-request" && !isTerminalWorkItem(this.#boards(input), card) && cardBelongsToRepository(card, input.repositoryId, input.repositoryName);
798
+ if (input.matchedItem && eligible(input.matchedItem) && cardBoardFlow(this.#boards(input), input.matchedItem).pullRequestReview !== void 0) return {
799
+ card: input.matchedItem,
800
+ reason: "authored"
801
+ };
802
+ if (!input.authorTrusted) return void 0;
803
+ for (const issueNumber of closingIssueNumbers(input.body, {
804
+ provider: "github",
805
+ repository: input.repositoryName
806
+ })) {
807
+ const card = items.find((item) => (item.externalSource?.externalId === canonicalSourceKey("issue", issueNumber) || item.externalSource?.externalId === legacySourceKey(input.repositoryId, "issue", issueNumber)) && eligible(item));
808
+ if (card) return {
809
+ card,
810
+ reason: "closes"
811
+ };
812
+ }
813
+ }
727
814
  async #relatedItem(orgId, projectId, repositoryId, repositoryFullName, issueNumber, pullRequestNumber, pullRequestHeadBranch, provenance, preferAuthoringItem = false) {
728
815
  await this.options.storage.copyLegacyCards({
729
816
  orgId,
@@ -747,7 +834,7 @@ var GithubRules = class {
747
834
  #resolveItem(items, repositoryId, repositoryFullName, issueNumber, pullRequestNumber, pullRequestHeadBranch, provenance) {
748
835
  if (provenance) return items.find((item) => item.id === provenance.workItemId);
749
836
  if (issueNumber) return items.find((item) => item.externalSource?.externalId === canonicalSourceKey("issue", issueNumber) && cardBelongsToRepository(item, repositoryId, repositoryFullName)) ?? items.find((item) => item.externalSource?.externalId === legacySourceKey(repositoryId, "issue", issueNumber));
750
- if (pullRequestNumber) return items.find((item) => item.externalSource?.externalId === canonicalSourceKey("pull-request", pullRequestNumber) && cardBelongsToRepository(item, repositoryId, repositoryFullName)) ?? items.find((item) => item.externalSource?.externalId === legacySourceKey(repositoryId, "pull-request", pullRequestNumber)) ?? (pullRequestHeadBranch ? items.find((item) => item.externalSource?.type !== "pull-request" && Object.values(item.sessions).some((session) => session.branch === pullRequestHeadBranch)) : void 0);
837
+ if (pullRequestNumber) return items.find((item) => item.externalSource?.externalId === canonicalSourceKey("pull-request", pullRequestNumber) && cardBelongsToRepository(item, repositoryId, repositoryFullName)) ?? items.find((item) => item.externalSource?.externalId === legacySourceKey(repositoryId, "pull-request", pullRequestNumber)) ?? items.find((item) => attachedPullRequestSource(item, repositoryId, repositoryFullName, pullRequestNumber)) ?? branchAuthoringItem(items, pullRequestHeadBranch, repositoryId, repositoryFullName);
751
838
  }
752
839
  };
753
840
  const RECONCILE_ERROR_SAMPLE_LIMIT = 5;
@@ -780,6 +867,16 @@ function reconcilablePullRequestNumber(item, repository) {
780
867
  const canonical = /^github-pr:(\d+)$/.exec(externalId);
781
868
  return canonical ? Number(canonical[1]) : void 0;
782
869
  }
870
+ /** The number of an attached pull request source, when it belongs to the given repository. */
871
+ function attachedPullRequestNumber(source, repository) {
872
+ if (source.integrationId !== "github") return void 0;
873
+ if (source.url) {
874
+ const match = /^https?:\/\/[^/]+\/(.+)\/pull\/(\d+)(?:[/?#]|$)/.exec(source.url);
875
+ return match && match[1] === repository.fullName ? Number(match[2]) : void 0;
876
+ }
877
+ const legacy = /^pull-request:github:(\d+):pull-request:(\d+)$/.exec(source.externalId);
878
+ return legacy && Number(legacy[1]) === repository.id ? Number(legacy[2]) : void 0;
879
+ }
783
880
  /**
784
881
  * Extracts the issue number a work item tracks, but only when the item
785
882
  * belongs to the given repository. Stricter than
@@ -997,6 +1094,7 @@ function createGithubPullRequestReconciler(options, fetchPullRequest) {
997
1094
  summary.repositories = scoped.length;
998
1095
  for (const repository of scoped) {
999
1096
  let cardsByNumber;
1097
+ let attachedOpen;
1000
1098
  let unanswered;
1001
1099
  try {
1002
1100
  const projects = await options.sourceControl.projectRepositories.listByExternalRepository({
@@ -1005,8 +1103,10 @@ function createGithubPullRequestReconciler(options, fetchPullRequest) {
1005
1103
  });
1006
1104
  if (projects.length === 0) continue;
1007
1105
  cardsByNumber = /* @__PURE__ */ new Map();
1106
+ attachedOpen = /* @__PURE__ */ new Set();
1008
1107
  unanswered = [];
1009
1108
  for (const project of projects) {
1109
+ await rules.loadBoards(project);
1010
1110
  const items = await options.storage.list({
1011
1111
  orgId: project.orgId,
1012
1112
  factoryProjectId: project.factoryProjectId
@@ -1017,6 +1117,10 @@ function createGithubPullRequestReconciler(options, fetchPullRequest) {
1017
1117
  item,
1018
1118
  author: unansweredAuthor
1019
1119
  });
1120
+ if (item.externalSource?.type !== "pull-request" && !isTerminalWorkItem(toBoardSource(options.boards).forProject(item), item)) for (const source of pullRequestSources(item)) {
1121
+ const number = attachedPullRequestNumber(source, repository);
1122
+ if (number !== void 0 && !["closed", "merged"].includes(source.state ?? "")) attachedOpen.add(number);
1123
+ }
1020
1124
  const pullRequestNumber = reconcilablePullRequestNumber(item, repository);
1021
1125
  if (!pullRequestNumber) continue;
1022
1126
  const metadata = {
@@ -1025,7 +1129,7 @@ function createGithubPullRequestReconciler(options, fetchPullRequest) {
1025
1129
  };
1026
1130
  const reconciliation = metadata[FACTORY_PULL_REQUEST_RECONCILIATION_KEY];
1027
1131
  const reconciledOutcome = reconciledPullRequestOutcome(metadata);
1028
- if (workItemPhaseSemantics(options.boards, item)?.kind === "terminal" && reconciledOutcome !== void 0 && reconciliation === reconciledOutcome) continue;
1132
+ if (workItemPhaseSemantics(toBoardSource(options.boards).forProject(item), item)?.kind === "terminal" && reconciledOutcome !== void 0 && reconciliation === reconciledOutcome) continue;
1029
1133
  const cards = cardsByNumber.get(pullRequestNumber) ?? [];
1030
1134
  cards.push(item);
1031
1135
  cardsByNumber.set(pullRequestNumber, cards);
@@ -1089,7 +1193,7 @@ function createGithubPullRequestReconciler(options, fetchPullRequest) {
1089
1193
  if (state.state !== "closed") continue;
1090
1194
  const cleanupFailures = /* @__PURE__ */ new Set();
1091
1195
  for (const card of cards) {
1092
- if (workItemPhaseSemantics(options.boards, card)?.kind !== "terminal") continue;
1196
+ if (workItemPhaseSemantics(toBoardSource(options.boards).forProject(card), card)?.kind !== "terminal") continue;
1093
1197
  try {
1094
1198
  await options.storage.supersedeDecisionsForWorkItem({
1095
1199
  orgId: card.orgId,
@@ -1123,6 +1227,24 @@ function createGithubPullRequestReconciler(options, fetchPullRequest) {
1123
1227
  } catch (error) {
1124
1228
  recordFailure(repository, error, pullRequestNumber);
1125
1229
  }
1230
+ for (const pullRequestNumber of attachedOpen) {
1231
+ if (cardsByNumber.has(pullRequestNumber)) continue;
1232
+ try {
1233
+ const state = await fetchPullRequest({
1234
+ installationId: repository.installationId,
1235
+ repository: repository.fullName,
1236
+ number: pullRequestNumber
1237
+ });
1238
+ summary.checked += 1;
1239
+ if (state?.state !== "closed") continue;
1240
+ await rules.ingest(reconciledClosedEvent(repository, pullRequestNumber, state));
1241
+ await retireReconciledSubscriptions(options.integrationStorage, repository, pullRequestNumber, state.merged);
1242
+ if (state.merged) summary.merged += 1;
1243
+ else summary.closed += 1;
1244
+ } catch (error) {
1245
+ recordFailure(repository, error, pullRequestNumber);
1246
+ }
1247
+ }
1126
1248
  }
1127
1249
  return summary;
1128
1250
  };
@@ -1137,7 +1259,6 @@ function githubRulesOptions(github, context) {
1137
1259
  storage: context.runtime.workItems,
1138
1260
  configVersion: context.runtime.configVersion,
1139
1261
  boards: context.runtime.boards,
1140
- intake: context.storage.intake,
1141
1262
  ...context.hooks?.onRepositoryPush ? { onRepositoryPush: context.hooks.onRepositoryPush } : {}
1142
1263
  };
1143
1264
  }
@@ -1152,6 +1273,6 @@ function attachGithubReconciler(github, context, fetchPullRequest) {
1152
1273
  if (!options) return void 0;
1153
1274
  return createGithubPullRequestReconciler(options, fetchPullRequest);
1154
1275
  }
1155
- export { GithubRules, RECONCILE_ERROR_SAMPLE_LIMIT, activitySignalFor, attachGithubReconciler, attachGithubRules, boardEventFor, cardBelongsToRepository, createGithubPullRequestReconciler, githubItemSources, githubRulesOptions, isFactoryGithubLogin, polledPullRequestEvent, reconcilableIssueNumber, reconciledClosedEvent, reconciledIssueClosedEvent, reconciledIssueOpenedEvent, reconciledIssueRelabeledEvent, sameStrings, sweepTrustLookup, trustedCollaborator };
1276
+ export { GithubRules, RECONCILE_ERROR_SAMPLE_LIMIT, activitySignalFor, attachGithubReconciler, attachGithubRules, attachedPullRequestSource, boardEventFor, cardBelongsToRepository, createGithubPullRequestReconciler, githubItemSources, githubRulesOptions, isFactoryGithubLogin, polledPullRequestEvent, reconcilableIssueNumber, reconciledClosedEvent, reconciledIssueClosedEvent, reconciledIssueOpenedEvent, reconciledIssueRelabeledEvent, sameStrings, sweepTrustLookup, trustedCollaborator };
1156
1277
 
1157
1278
  //# sourceMappingURL=rules.js.map