@mastra/factory 0.29.0-alpha.4 → 0.29.0-alpha.5

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 (315) hide show
  1. package/README.md +119 -60
  2. package/dist/boards/board-events.d.ts +89 -9
  3. package/dist/boards/board-events.d.ts.map +1 -1
  4. package/dist/boards/board-events.js +176 -13
  5. package/dist/boards/board-events.js.map +1 -1
  6. package/dist/boards/board-writes.d.ts.map +1 -1
  7. package/dist/boards/board-writes.js +14 -1
  8. package/dist/boards/board-writes.js.map +1 -1
  9. package/dist/boards/busy-card.d.ts +11 -0
  10. package/dist/boards/busy-card.d.ts.map +1 -0
  11. package/dist/boards/busy-card.js +15 -0
  12. package/dist/boards/busy-card.js.map +1 -0
  13. package/dist/boards/card-facts.d.ts.map +1 -1
  14. package/dist/boards/card-facts.js +18 -4
  15. package/dist/boards/card-facts.js.map +1 -1
  16. package/dist/boards/index.d.ts +2 -1
  17. package/dist/boards/index.d.ts.map +1 -1
  18. package/dist/boards/index.js +3 -2
  19. package/dist/boards/intake-dry-run.d.ts +109 -0
  20. package/dist/boards/intake-dry-run.d.ts.map +1 -0
  21. package/dist/boards/intake-dry-run.js +158 -0
  22. package/dist/boards/intake-dry-run.js.map +1 -0
  23. package/dist/boards/intake-lookup.d.ts +32 -0
  24. package/dist/boards/intake-lookup.d.ts.map +1 -0
  25. package/dist/boards/intake-lookup.js +12 -0
  26. package/dist/boards/intake-lookup.js.map +1 -0
  27. package/dist/boards/intake-order.d.ts +32 -0
  28. package/dist/boards/intake-order.d.ts.map +1 -0
  29. package/dist/boards/intake-order.js +60 -0
  30. package/dist/boards/intake-order.js.map +1 -0
  31. package/dist/boards/intake-routing.d.ts +246 -35
  32. package/dist/boards/intake-routing.d.ts.map +1 -1
  33. package/dist/boards/intake-routing.js +437 -52
  34. package/dist/boards/intake-routing.js.map +1 -1
  35. package/dist/boards/reroute.d.ts +7 -3
  36. package/dist/boards/reroute.d.ts.map +1 -1
  37. package/dist/boards/reroute.js +21 -4
  38. package/dist/boards/reroute.js.map +1 -1
  39. package/dist/boards/semantics.d.ts +8 -28
  40. package/dist/boards/semantics.d.ts.map +1 -1
  41. package/dist/boards/semantics.js +7 -39
  42. package/dist/boards/semantics.js.map +1 -1
  43. package/dist/boards/work-routes.d.ts +31 -11
  44. package/dist/boards/work-routes.d.ts.map +1 -1
  45. package/dist/boards/work-routes.js +104 -20
  46. package/dist/boards/work-routes.js.map +1 -1
  47. package/dist/factory.d.ts +27 -1
  48. package/dist/factory.d.ts.map +1 -1
  49. package/dist/factory.js +31 -9
  50. package/dist/factory.js.map +1 -1
  51. package/dist/integrations/base.d.ts +9 -0
  52. package/dist/integrations/base.d.ts.map +1 -1
  53. package/dist/integrations/dependabot/rules.js +1 -1
  54. package/dist/integrations/dependabot/rules.js.map +1 -1
  55. package/dist/integrations/github/default-rules.d.ts +22 -1
  56. package/dist/integrations/github/default-rules.d.ts.map +1 -1
  57. package/dist/integrations/github/default-rules.js +23 -5
  58. package/dist/integrations/github/default-rules.js.map +1 -1
  59. package/dist/integrations/github/intake-lookup.d.ts +16 -0
  60. package/dist/integrations/github/intake-lookup.d.ts.map +1 -0
  61. package/dist/integrations/github/intake-lookup.js +76 -0
  62. package/dist/integrations/github/intake-lookup.js.map +1 -0
  63. package/dist/integrations/github/integration.d.ts +19 -5
  64. package/dist/integrations/github/integration.d.ts.map +1 -1
  65. package/dist/integrations/github/integration.js +21 -6
  66. package/dist/integrations/github/integration.js.map +1 -1
  67. package/dist/integrations/github/review-status-reporter.js +1 -1
  68. package/dist/integrations/github/routes.d.ts.map +1 -1
  69. package/dist/integrations/github/routes.js +14 -2
  70. package/dist/integrations/github/routes.js.map +1 -1
  71. package/dist/integrations/github/rules.d.ts +12 -1
  72. package/dist/integrations/github/rules.d.ts.map +1 -1
  73. package/dist/integrations/github/rules.js +218 -30
  74. package/dist/integrations/github/rules.js.map +1 -1
  75. package/dist/integrations/github/sandbox.d.ts.map +1 -1
  76. package/dist/integrations/github/sandbox.js +30 -4
  77. package/dist/integrations/github/sandbox.js.map +1 -1
  78. package/dist/integrations/gitlab/default-rules.d.ts.map +1 -1
  79. package/dist/integrations/gitlab/default-rules.js +19 -3
  80. package/dist/integrations/gitlab/default-rules.js.map +1 -1
  81. package/dist/integrations/gitlab/intake-lookup.d.ts +17 -0
  82. package/dist/integrations/gitlab/intake-lookup.d.ts.map +1 -0
  83. package/dist/integrations/gitlab/intake-lookup.js +85 -0
  84. package/dist/integrations/gitlab/intake-lookup.js.map +1 -0
  85. package/dist/integrations/gitlab/integration.d.ts +5 -0
  86. package/dist/integrations/gitlab/integration.d.ts.map +1 -1
  87. package/dist/integrations/gitlab/integration.js +21 -1
  88. package/dist/integrations/gitlab/integration.js.map +1 -1
  89. package/dist/integrations/gitlab/rules.d.ts +20 -0
  90. package/dist/integrations/gitlab/rules.d.ts.map +1 -1
  91. package/dist/integrations/gitlab/rules.js +108 -12
  92. package/dist/integrations/gitlab/rules.js.map +1 -1
  93. package/dist/integrations/incidentio/rules.d.ts.map +1 -1
  94. package/dist/integrations/incidentio/rules.js +14 -4
  95. package/dist/integrations/incidentio/rules.js.map +1 -1
  96. package/dist/integrations/jira/rules.d.ts.map +1 -1
  97. package/dist/integrations/jira/rules.js +12 -2
  98. package/dist/integrations/jira/rules.js.map +1 -1
  99. package/dist/integrations/linear/rules.d.ts.map +1 -1
  100. package/dist/integrations/linear/rules.js +13 -2
  101. package/dist/integrations/linear/rules.js.map +1 -1
  102. package/dist/integrations/platform/github/event-worker.js +9 -1
  103. package/dist/integrations/platform/github/event-worker.js.map +1 -1
  104. package/dist/integrations/platform/github/integration.d.ts +6 -6
  105. package/dist/integrations/platform/github/integration.d.ts.map +1 -1
  106. package/dist/integrations/platform/github/integration.js +22 -3
  107. package/dist/integrations/platform/github/integration.js.map +1 -1
  108. package/dist/integrations/pull-request-head.d.ts +1 -1
  109. package/dist/integrations/pull-request-head.d.ts.map +1 -1
  110. package/dist/integrations/pull-request-head.js +5 -0
  111. package/dist/integrations/pull-request-head.js.map +1 -1
  112. package/dist/integrations/vanta/rules.js +1 -1
  113. package/dist/integrations/vanta/rules.js.map +1 -1
  114. package/dist/routes/board-snapshot.d.ts +8 -1
  115. package/dist/routes/board-snapshot.d.ts.map +1 -1
  116. package/dist/routes/board-snapshot.js +33 -7
  117. package/dist/routes/board-snapshot.js.map +1 -1
  118. package/dist/routes/boards.js.map +1 -1
  119. package/dist/routes/config.d.ts +6 -0
  120. package/dist/routes/config.d.ts.map +1 -1
  121. package/dist/routes/config.js +5 -2
  122. package/dist/routes/config.js.map +1 -1
  123. package/dist/routes/contracts.d.ts +1258 -0
  124. package/dist/routes/contracts.d.ts.map +1 -1
  125. package/dist/routes/contracts.js +586 -13
  126. package/dist/routes/contracts.js.map +1 -1
  127. package/dist/routes/intake-check.d.ts +92 -0
  128. package/dist/routes/intake-check.d.ts.map +1 -0
  129. package/dist/routes/intake-check.js +322 -0
  130. package/dist/routes/intake-check.js.map +1 -0
  131. package/dist/routes/intake-routes.d.ts +10 -2
  132. package/dist/routes/intake-routes.d.ts.map +1 -1
  133. package/dist/routes/intake-routes.js +292 -11
  134. package/dist/routes/intake-routes.js.map +1 -1
  135. package/dist/routes/mastra-gateway.d.ts.map +1 -1
  136. package/dist/routes/mastra-gateway.js +3 -2
  137. package/dist/routes/mastra-gateway.js.map +1 -1
  138. package/dist/routes/project-sessions.d.ts +49 -0
  139. package/dist/routes/project-sessions.d.ts.map +1 -0
  140. package/dist/routes/project-sessions.js +93 -0
  141. package/dist/routes/project-sessions.js.map +1 -0
  142. package/dist/routes/run-queue.d.ts +6 -6
  143. package/dist/routes/run-queue.d.ts.map +1 -1
  144. package/dist/routes/run-queue.js +28 -8
  145. package/dist/routes/run-queue.js.map +1 -1
  146. package/dist/routes/session-rows.d.ts +30 -0
  147. package/dist/routes/session-rows.d.ts.map +1 -0
  148. package/dist/routes/session-rows.js +98 -0
  149. package/dist/routes/session-rows.js.map +1 -0
  150. package/dist/routes/source-control-sessions.d.ts +4 -5
  151. package/dist/routes/source-control-sessions.d.ts.map +1 -1
  152. package/dist/routes/source-control-sessions.js +8 -87
  153. package/dist/routes/source-control-sessions.js.map +1 -1
  154. package/dist/routes/surface.d.ts +13 -1
  155. package/dist/routes/surface.d.ts.map +1 -1
  156. package/dist/routes/surface.js +24 -26
  157. package/dist/routes/surface.js.map +1 -1
  158. package/dist/routes/work-item-starters.d.ts +1 -0
  159. package/dist/routes/work-item-starters.d.ts.map +1 -1
  160. package/dist/routes/work-item-starters.js +14 -2
  161. package/dist/routes/work-item-starters.js.map +1 -1
  162. package/dist/routes/work-items.d.ts +13 -0
  163. package/dist/routes/work-items.d.ts.map +1 -1
  164. package/dist/routes/work-items.js +87 -34
  165. package/dist/routes/work-items.js.map +1 -1
  166. package/dist/rules/dispatch-errors.js +1 -1
  167. package/dist/rules/dispatch-errors.js.map +1 -1
  168. package/dist/rules/dispatcher.d.ts +39 -7
  169. package/dist/rules/dispatcher.d.ts.map +1 -1
  170. package/dist/rules/dispatcher.js +361 -122
  171. package/dist/rules/dispatcher.js.map +1 -1
  172. package/dist/rules/event-rules.d.ts +69 -1
  173. package/dist/rules/event-rules.d.ts.map +1 -1
  174. package/dist/rules/event-rules.js +151 -5
  175. package/dist/rules/event-rules.js.map +1 -1
  176. package/dist/rules/review-source-tool.js +1 -1
  177. package/dist/rules/rule-group.d.ts +18 -1
  178. package/dist/rules/rule-group.d.ts.map +1 -1
  179. package/dist/rules/rule-group.js +52 -8
  180. package/dist/rules/rule-group.js.map +1 -1
  181. package/dist/rules/run-bearing.d.ts +8 -0
  182. package/dist/rules/run-bearing.d.ts.map +1 -0
  183. package/dist/rules/run-bearing.js +11 -0
  184. package/dist/rules/run-bearing.js.map +1 -0
  185. package/dist/rules/start-coordinator.d.ts +2 -3
  186. package/dist/rules/start-coordinator.d.ts.map +1 -1
  187. package/dist/rules/start-coordinator.js +26 -34
  188. package/dist/rules/start-coordinator.js.map +1 -1
  189. package/dist/rules/tools.js +2 -1
  190. package/dist/rules/tools.js.map +1 -1
  191. package/dist/rules/transition-service.d.ts +16 -0
  192. package/dist/rules/transition-service.d.ts.map +1 -1
  193. package/dist/rules/transition-service.js +34 -10
  194. package/dist/rules/transition-service.js.map +1 -1
  195. package/dist/rules/types.d.ts +18 -5
  196. package/dist/rules/types.d.ts.map +1 -1
  197. package/dist/rules/types.js +2 -1
  198. package/dist/rules/types.js.map +1 -1
  199. package/dist/rules/update-fold.d.ts +32 -0
  200. package/dist/rules/update-fold.d.ts.map +1 -0
  201. package/dist/rules/update-fold.js +60 -0
  202. package/dist/rules/update-fold.js.map +1 -0
  203. package/dist/rules/validation.d.ts.map +1 -1
  204. package/dist/rules/validation.js +54 -6
  205. package/dist/rules/validation.js.map +1 -1
  206. package/dist/session/resource-migration.d.ts.map +1 -1
  207. package/dist/session/resource-migration.js +2 -3
  208. package/dist/session/resource-migration.js.map +1 -1
  209. package/dist/session/run-audit.d.ts +9 -0
  210. package/dist/session/run-audit.d.ts.map +1 -1
  211. package/dist/session/run-audit.js +9 -1
  212. package/dist/session/run-audit.js.map +1 -1
  213. package/dist/session/self-review-gate.d.ts +26 -2
  214. package/dist/session/self-review-gate.d.ts.map +1 -1
  215. package/dist/session/self-review-gate.js +60 -10
  216. package/dist/session/self-review-gate.js.map +1 -1
  217. package/dist/session/session-row-query.d.ts +25 -0
  218. package/dist/session/session-row-query.d.ts.map +1 -0
  219. package/dist/session/session-row-query.js +47 -0
  220. package/dist/session/session-row-query.js.map +1 -0
  221. package/dist/session/session-row-status.d.ts +16 -0
  222. package/dist/session/session-row-status.d.ts.map +1 -0
  223. package/dist/session/session-row-status.js +14 -0
  224. package/dist/session/session-row-status.js.map +1 -0
  225. package/dist/session/session-row-view.d.ts +14 -0
  226. package/dist/session/session-row-view.d.ts.map +1 -0
  227. package/dist/session/session-row-view.js +125 -0
  228. package/dist/session/session-row-view.js.map +1 -0
  229. package/dist/session/session-rows.d.ts +46 -0
  230. package/dist/session/session-rows.d.ts.map +1 -0
  231. package/dist/session/session-rows.js +232 -0
  232. package/dist/session/session-rows.js.map +1 -0
  233. package/dist/session/session-threads.d.ts.map +1 -1
  234. package/dist/session/session-threads.js +1 -0
  235. package/dist/session/session-threads.js.map +1 -1
  236. package/dist/session/source-control-tools.d.ts +14 -7
  237. package/dist/session/source-control-tools.d.ts.map +1 -1
  238. package/dist/session/source-control-tools.js +10 -2
  239. package/dist/session/source-control-tools.js.map +1 -1
  240. package/dist/session/work-item-repository.d.ts +44 -0
  241. package/dist/session/work-item-repository.d.ts.map +1 -1
  242. package/dist/session/work-item-repository.js +80 -1
  243. package/dist/session/work-item-repository.js.map +1 -1
  244. package/dist/session/work-item-state-processor.d.ts.map +1 -1
  245. package/dist/session/work-item-state-processor.js +44 -8
  246. package/dist/session/work-item-state-processor.js.map +1 -1
  247. package/dist/storage/domains/audit/actions.d.ts +2 -1
  248. package/dist/storage/domains/audit/actions.d.ts.map +1 -1
  249. package/dist/storage/domains/audit/actions.js +3 -1
  250. package/dist/storage/domains/audit/actions.js.map +1 -1
  251. package/dist/storage/domains/boards/base.d.ts +162 -14
  252. package/dist/storage/domains/boards/base.d.ts.map +1 -1
  253. package/dist/storage/domains/boards/base.js +415 -41
  254. package/dist/storage/domains/boards/base.js.map +1 -1
  255. package/dist/storage/domains/boards/schemas.d.ts +6 -0
  256. package/dist/storage/domains/boards/schemas.d.ts.map +1 -1
  257. package/dist/storage/domains/boards/schemas.js +95 -1
  258. package/dist/storage/domains/boards/schemas.js.map +1 -1
  259. package/dist/storage/domains/projects/base.d.ts.map +1 -1
  260. package/dist/storage/domains/projects/base.js +19 -10
  261. package/dist/storage/domains/projects/base.js.map +1 -1
  262. package/dist/storage/domains/source-control/base.d.ts.map +1 -1
  263. package/dist/storage/domains/source-control/base.js +5 -2
  264. package/dist/storage/domains/source-control/base.js.map +1 -1
  265. package/dist/storage/domains/work-items/base.d.ts +48 -17
  266. package/dist/storage/domains/work-items/base.d.ts.map +1 -1
  267. package/dist/storage/domains/work-items/base.js +128 -75
  268. package/dist/storage/domains/work-items/base.js.map +1 -1
  269. package/dist/storage/domains/work-items/cards.d.ts +3 -2
  270. package/dist/storage/domains/work-items/cards.d.ts.map +1 -1
  271. package/dist/storage/domains/work-items/cards.js +4 -2
  272. package/dist/storage/domains/work-items/cards.js.map +1 -1
  273. package/dist/storage/probed-list.d.ts +11 -0
  274. package/dist/storage/probed-list.d.ts.map +1 -0
  275. package/dist/storage/probed-list.js +52 -0
  276. package/dist/storage/probed-list.js.map +1 -0
  277. package/dist/tools/audit-events-tool.d.ts +2 -2
  278. package/dist/tools/read-tools.d.ts +0 -3
  279. package/dist/tools/read-tools.d.ts.map +1 -1
  280. package/dist/tools/read-tools.js +4 -42
  281. package/dist/tools/read-tools.js.map +1 -1
  282. package/dist/tools/session-tools.d.ts.map +1 -1
  283. package/dist/tools/session-tools.js +5 -9
  284. package/dist/tools/session-tools.js.map +1 -1
  285. package/dist/tools/work-item-tools.d.ts +3 -0
  286. package/dist/tools/work-item-tools.d.ts.map +1 -1
  287. package/dist/tools/work-item-tools.js +8 -4
  288. package/dist/tools/work-item-tools.js.map +1 -1
  289. package/dist/work-item-branch.d.ts +4 -0
  290. package/dist/work-item-branch.d.ts.map +1 -1
  291. package/dist/work-item-branch.js +7 -1
  292. package/dist/work-item-branch.js.map +1 -1
  293. package/dist/work-items/card-context-sessions.d.ts +50 -0
  294. package/dist/work-items/card-context-sessions.d.ts.map +1 -0
  295. package/dist/work-items/card-context-sessions.js +84 -0
  296. package/dist/work-items/card-context-sessions.js.map +1 -0
  297. package/dist/work-items/change-requests.d.ts +87 -0
  298. package/dist/work-items/change-requests.d.ts.map +1 -0
  299. package/dist/work-items/change-requests.js +134 -0
  300. package/dist/work-items/change-requests.js.map +1 -0
  301. package/dist/work-items/create-work-item.d.ts +7 -2
  302. package/dist/work-items/create-work-item.d.ts.map +1 -1
  303. package/dist/work-items/create-work-item.js +44 -13
  304. package/dist/work-items/create-work-item.js.map +1 -1
  305. package/dist/work-items/list-related-cards.d.ts.map +1 -1
  306. package/dist/work-items/list-related-cards.js +1 -1
  307. package/dist/work-items/list-related-cards.js.map +1 -1
  308. package/dist/work-items/providers/github.d.ts.map +1 -1
  309. package/dist/work-items/providers/github.js +1 -0
  310. package/dist/work-items/providers/github.js.map +1 -1
  311. package/factory-skills/factory-build/SKILL.md +1 -1
  312. package/factory-skills/factory-plan/SKILL.md +5 -3
  313. package/factory-skills/factory-self-review/SKILL.md +2 -0
  314. package/factory-skills/factory-triage/SKILL.md +1 -1
  315. package/package.json +8 -8
@@ -1,11 +1,13 @@
1
- import { closeReason } from "../../boards/board-events.js";
1
+ import { closeReason, listChange, updateEventFacts } from "../../boards/board-events.js";
2
2
  import "../../boards/index.js";
3
3
  import { toBoardSource } from "../../boards/project-boards.js";
4
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
5
  import { recordIntakeEvent } from "../../metrics/intake.js";
6
+ import { isPullRequestCard, pullRequestSources, trustedPullRequestSource } from "../../work-items/change-requests.js";
7
+ import { boardEventHandlers, boardForWorkItem, cardBoardFlow, cardOwnsBranch, eventBoardId, intakeTarget, isTerminalWorkItem, newestOwnedOpenPullRequestNumber, rulePhaseField, withBoardClosure, withBoardFlow, workItemPhaseSemantics } from "../../boards/semantics.js";
7
8
  import { requestsChangesVerdict } from "../../review-verdict.js";
8
- import { deploymentOwnsEvent, routeEventDecision, selectEventRule, withoutRouteEvent } from "../../rules/event-rules.js";
9
+ import { commitCardUpdate, deploymentOwnsEvent, routeEventDecision, selectEventRule, withPullRequestTrust, withoutRouteEvent } from "../../rules/event-rules.js";
10
+ import { authorTrusted } from "../../rules/types.js";
9
11
  import { assertFactoryDecisionTarget, validateFactoryRuleDecisions } from "../../rules/validation.js";
10
12
  import { FACTORY_PULL_REQUEST_RECONCILIATION_KEY, WorkItemUpdateConflictError } from "../../storage/domains/work-items/base.js";
11
13
  import { primarySource } from "../../storage/domains/work-items/work-item-schema.js";
@@ -46,6 +48,9 @@ function number(value) {
46
48
  function boolean(value) {
47
49
  return typeof value === "boolean" ? value : void 0;
48
50
  }
51
+ function stringList(value) {
52
+ return Array.isArray(value) ? value.filter((entry) => typeof entry === "string") : [];
53
+ }
49
54
  function actorLogins(value) {
50
55
  if (!Array.isArray(value)) return [];
51
56
  return value.flatMap((actor) => {
@@ -97,11 +102,47 @@ function activitySignalFor(context) {
97
102
  authorTrusted: context.actor.trusted
98
103
  };
99
104
  }
105
+ /**
106
+ * A label, assignee or reopen delivery filed again as its item's author opening it, so intake
107
+ * judges the author's trust, never that of whoever labelled or reopened it. Undefined when the
108
+ * delivery doesn't name the author.
109
+ */
110
+ function openedByAuthor(parsed, item) {
111
+ const author = object(item?.user);
112
+ const login = string(author?.login);
113
+ if (!author || !login) return void 0;
114
+ return {
115
+ arrival: {
116
+ ...parsed,
117
+ deliveryId: `${parsed.deliveryId}:intake`,
118
+ payload: {
119
+ ...parsed.payload,
120
+ action: "opened",
121
+ sender: author
122
+ }
123
+ },
124
+ login
125
+ };
126
+ }
127
+ /**
128
+ * Whether the delivery's pull request comes from someone with write access, or from Factory: its
129
+ * own card's stamp, the stamp recorded when it joined the card, else its author acting now.
130
+ */
131
+ function githubPullRequestTrusted(context) {
132
+ const { pullRequest, item } = context;
133
+ if (!pullRequest) return false;
134
+ if (pullRequest.factoryAuthored) return true;
135
+ if (item && isPullRequestCard(item)) return authorTrusted(item.metadata);
136
+ const attached = item ? pullRequestSources(item).find((source) => isRepositoryPullRequest(source, context.repository.id, context.repository.fullName, pullRequest.number)) : void 0;
137
+ if (item && attached) return trustedPullRequestSource(item, attached);
138
+ return context.actor.type === "github" && context.actor.trusted && pullRequest.author !== void 0 && context.actor.login === pullRequest.author;
139
+ }
100
140
  /** The status change a GitHub delivery reports. `not_planned` and `duplicate` closes are canceled work. */
101
141
  function boardEventFor(context) {
102
142
  switch (context.event) {
103
- case "issueOpened": return { type: "work_item.created" };
143
+ case "issueOpened": return { type: "work_item.opened" };
104
144
  case "issueReopened": return { type: "work_item.reopened" };
145
+ case "pullRequestReopened": return { type: "pull_request.reopened" };
105
146
  case "issueClosed": {
106
147
  const stateReason = context.issue?.stateReason;
107
148
  return {
@@ -134,6 +175,7 @@ function eventName(parsed) {
134
175
  if (parsed.event === "pull_request" && action === "synchronize") return "pullRequestUpdated";
135
176
  if (parsed.event === "pull_request" && action === "closed") return boolean(object(parsed.payload.pull_request)?.merged) ? "pullRequestMerged" : "pullRequestClosed";
136
177
  if (parsed.event === "pull_request" && action === "review_requested") return "pullRequestReviewRequested";
178
+ if (parsed.event === "pull_request" && action === "reopened") return "pullRequestReopened";
137
179
  if (parsed.event === "pull_request_review" && action === "submitted") return "pullRequestReviewSubmitted";
138
180
  if (parsed.event === "pull_request_review" && action === "edited") {
139
181
  const review = object(parsed.payload.review);
@@ -256,7 +298,8 @@ async function githubActor(github, input) {
256
298
  type: "github",
257
299
  login: input.login,
258
300
  trusted,
259
- factoryAuthored: input.factoryAuthored
301
+ factoryAuthored: input.factoryAuthored,
302
+ human: input.human
260
303
  };
261
304
  }
262
305
  function pullRequestProvenance(data, factoryProjectId) {
@@ -269,6 +312,12 @@ function pullRequestProvenance(data, factoryProjectId) {
269
312
  }
270
313
  /** Identity under which label and assignee syncs are recorded. */
271
314
  const CARD_SYNC_USER_ID = "factory-rule-dispatcher";
315
+ const PULL_REQUEST_UPDATE_ACTIONS = /* @__PURE__ */ new Set([
316
+ "labeled",
317
+ "unlabeled",
318
+ "assigned",
319
+ "unassigned"
320
+ ]);
272
321
  /**
273
322
  * Whether a login is Factory itself. Prefers the resolved identity, which is
274
323
  * observed from Factory's own writes, and falls back to the configured slug.
@@ -308,42 +357,78 @@ var GithubRules = class {
308
357
  return slug ? `${slug.toLowerCase()}[bot]` : void 0;
309
358
  }
310
359
  /**
311
- * `labeled` / `unlabeled` are not rule events: the card's label metadata is refreshed and the card
312
- * stays where it is. Intake routes placed it when it arrived; a label change doesn't move it.
360
+ * `labeled` / `unlabeled` refresh the card's label metadata, then hand what changed to the card's
361
+ * routes as a `work_item.updated` event. Only routes naming the event move the card.
313
362
  */
314
363
  async #syncIssueLabels(parsed, repositoryId, repositoryName, project) {
315
364
  const issue = object(parsed.payload.issue);
316
365
  const issueNumber = issue?.pull_request === void 0 ? number(issue?.number) : void 0;
317
366
  if (!issueNumber) return { status: "ignored" };
318
367
  const found = await this.#relatedItem(project.orgId, project.factoryProjectId, repositoryId, repositoryName, issueNumber, void 0, void 0, null);
319
- if (!found || found.externalSource?.type !== "issue") return { status: "ignored" };
368
+ if (!found) return this.#intakeIfNotRouted(parsed, repositoryId, repositoryName, project, issueNumber);
369
+ if (found.externalSource?.type !== "issue") return { status: "ignored" };
320
370
  const labels = labelNames(issue?.labels);
321
- if (sameLabels(cardLabels(found), labels)) return { status: "ignored" };
322
- let updated;
371
+ const before = cardLabels(found);
372
+ const updatedAt = string(issue?.updated_at);
373
+ const payloadTime = timestamp(updatedAt);
374
+ const storedTime = timestamp(found.workItem.metadata?.githubLabelsUpdatedAt);
375
+ if (payloadTime !== void 0 && storedTime !== void 0 && payloadTime < storedTime) return { status: "ignored" };
376
+ const advancesTime = payloadTime !== void 0 && (storedTime === void 0 || payloadTime > storedTime);
377
+ if (sameLabels(before, labels) && !advancesTime) return { status: "ignored" };
378
+ if (await this.#commitUpdate(parsed, repositoryName, project, found, updateEventFacts("work_item.updated", { labels: listChange(before, labels) })) === "ignored") return { status: "ignored" };
323
379
  try {
324
- updated = await this.options.storage.syncCard({
380
+ await this.options.storage.syncCard({
325
381
  orgId: found.orgId,
326
382
  id: found.id,
327
383
  userId: CARD_SYNC_USER_ID,
328
- patch: { metadata: { labels } },
384
+ patch: { metadata: {
385
+ labels,
386
+ ...payloadTime !== void 0 ? { githubLabelsUpdatedAt: updatedAt } : {}
387
+ } },
329
388
  expectedSourceRevision: primarySource(found)?.revision
330
389
  });
331
390
  } catch (error) {
332
391
  if (!(error instanceof WorkItemUpdateConflictError)) throw error;
333
- return { status: "ignored" };
334
392
  }
335
- return { status: updated ? "committed" : "ignored" };
393
+ return { status: "committed" };
336
394
  }
337
395
  /**
338
- * `assigned` / `unassigned` are not rule events either: an existing issue card only mirrors the
339
- * issue's full assignee list. No card is created, moved, or handed to a run.
396
+ * Hands a label or assignee change to the card's routes. A change Factory made itself is the
397
+ * card's own session's, held until its run ends.
398
+ */
399
+ async #commitUpdate(parsed, repositoryName, project, item, event) {
400
+ const installationId = number(object(parsed.payload.installation)?.id);
401
+ const login = string(object(parsed.payload.sender)?.login);
402
+ if (!installationId || !login || !event) return "unchanged";
403
+ const fromCardSession = this.#isFactoryLogin(login);
404
+ const actor = await githubActor(this.options.github, {
405
+ installationId,
406
+ repository: repositoryName,
407
+ login,
408
+ factoryAuthored: fromCardSession,
409
+ human: string(object(parsed.payload.sender)?.type) === "User"
410
+ });
411
+ return await commitCardUpdate(this.options.storage, {
412
+ orgId: project.orgId,
413
+ factoryProjectId: project.factoryProjectId,
414
+ item,
415
+ ingress: { identity: `${installationId}:${parsed.deliveryId}` },
416
+ actor: { ...actor },
417
+ event,
418
+ fromCardSession
419
+ });
420
+ }
421
+ /**
422
+ * `assigned` / `unassigned` mirror the issue's full assignee list onto an existing issue card,
423
+ * then hand what changed to the card's routes as a `work_item.updated` event. No card is created.
340
424
  */
341
425
  async #syncIssueAssignees(parsed, repositoryId, repositoryName, project) {
342
426
  const issue = object(parsed.payload.issue);
343
427
  const issueNumber = issue?.pull_request === void 0 ? number(issue?.number) : void 0;
344
428
  if (!issueNumber) return { status: "ignored" };
345
429
  const item = await this.#relatedItem(project.orgId, project.factoryProjectId, repositoryId, repositoryName, issueNumber, void 0, void 0, null);
346
- if (!item || item.externalSource?.type !== "issue") return { status: "ignored" };
430
+ if (!item) return this.#intakeIfNotRouted(parsed, repositoryId, repositoryName, project, issueNumber);
431
+ if (item.externalSource?.type !== "issue") return { status: "ignored" };
347
432
  const assignees = actorLogins(issue?.assignees);
348
433
  const updatedAt = string(issue?.updated_at);
349
434
  const payloadTime = timestamp(updatedAt);
@@ -351,8 +436,9 @@ var GithubRules = class {
351
436
  if (payloadTime !== void 0 && storedTime !== void 0 && payloadTime < storedTime) return { status: "ignored" };
352
437
  const advancesTime = payloadTime !== void 0 && (storedTime === void 0 || payloadTime > storedTime);
353
438
  if (sameStrings(item.workItem.metadata?.assignees, assignees) && !advancesTime) return { status: "ignored" };
439
+ if (await this.#commitUpdate(parsed, repositoryName, project, item, updateEventFacts("work_item.updated", { assignees: listChange(stringList(item.workItem.metadata?.assignees), assignees) })) === "ignored") return { status: "ignored" };
354
440
  try {
355
- return { status: await this.options.storage.syncCard({
441
+ await this.options.storage.syncCard({
356
442
  orgId: item.orgId,
357
443
  id: item.id,
358
444
  userId: CARD_SYNC_USER_ID,
@@ -361,11 +447,11 @@ var GithubRules = class {
361
447
  ...payloadTime !== void 0 ? { githubAssigneesUpdatedAt: updatedAt } : {}
362
448
  } },
363
449
  expectedSourceRevision: primarySource(item)?.revision
364
- }) ? "committed" : "ignored" };
450
+ });
365
451
  } catch (error) {
366
452
  if (!(error instanceof WorkItemUpdateConflictError)) throw error;
367
- return { status: "ignored" };
368
453
  }
454
+ return { status: "committed" };
369
455
  }
370
456
  /** A reopened pull request reads open again on every card that holds it; clearing the sweep's settled stamp lets a later close replay. */
371
457
  async #syncReopenedPullRequest(parsed, repositoryId, repositoryName, project) {
@@ -418,10 +504,81 @@ var GithubRules = class {
418
504
  });
419
505
  return Boolean(updated);
420
506
  }
507
+ /**
508
+ * A label or assignee added to or removed from a pull request, handed to the routes of the card it
509
+ * belongs to as `pull_request.updated`, on the pull request's own card or the issue card it is
510
+ * attached to. GitHub names the one label or assignee that changed.
511
+ */
512
+ async #pullRequestUpdate(parsed, repositoryId, repositoryName, project) {
513
+ const pullRequestNumber = number(object(parsed.payload.pull_request)?.number);
514
+ if (!pullRequestNumber) return { status: "ignored" };
515
+ const action = string(parsed.payload.action);
516
+ const name = action === "labeled" || action === "unlabeled" ? string(object(parsed.payload.label)?.name) : string(object(parsed.payload.assignee)?.login);
517
+ if (!name) return { status: "ignored" };
518
+ const item = await this.#relatedItem(project.orgId, project.factoryProjectId, repositoryId, repositoryName, void 0, pullRequestNumber, void 0, null);
519
+ if (!item) return this.#intakeIfNotRouted(parsed, repositoryId, repositoryName, project, pullRequestNumber, "pull-request");
520
+ const ownCard = item.externalSource?.type === "pull-request";
521
+ const attached = ownCard ? void 0 : attachedPullRequestSource(item, repositoryId, repositoryName, pullRequestNumber);
522
+ if (!ownCard && !attached) return { status: "ignored" };
523
+ const labelChange = action === "labeled" || action === "unlabeled";
524
+ const delta = action === "labeled" || action === "assigned" ? {
525
+ added: [name],
526
+ removed: []
527
+ } : {
528
+ added: [],
529
+ removed: [name]
530
+ };
531
+ if (attached) {
532
+ const event = updateEventFacts("pull_request.updated", labelChange ? { labels: delta } : { assignees: delta });
533
+ const queued = await this.#commitUpdate(parsed, repositoryName, project, item, event && {
534
+ ...event,
535
+ pullRequest: attached.externalId,
536
+ pullRequestTrusted: trustedPullRequestSource(item, attached)
537
+ });
538
+ return { status: queued === "committed" || queued === "replayed" ? "committed" : "ignored" };
539
+ }
540
+ const pullRequest = object(parsed.payload.pull_request);
541
+ const before = {
542
+ labels: cardLabels(item),
543
+ assignees: stringList(item.workItem.metadata?.assignees)
544
+ };
545
+ const applied = (list) => delta.added.length ? [...list, ...delta.added] : list.filter((entry) => entry.toLowerCase() !== name.toLowerCase());
546
+ const after = {
547
+ labels: Array.isArray(pullRequest?.labels) ? labelNames(pullRequest.labels) : labelChange ? applied(before.labels) : before.labels,
548
+ assignees: Array.isArray(pullRequest?.assignees) ? actorLogins(pullRequest.assignees) : labelChange ? before.assignees : applied(before.assignees)
549
+ };
550
+ const event = updateEventFacts("pull_request.updated", {
551
+ labels: listChange(before.labels, after.labels),
552
+ assignees: listChange(before.assignees, after.assignees)
553
+ });
554
+ const queued = await this.#commitUpdate(parsed, repositoryName, project, item, event && {
555
+ ...event,
556
+ pullRequestTrusted: authorTrusted(item.metadata)
557
+ });
558
+ if (queued === "ignored") return { status: "ignored" };
559
+ try {
560
+ await this.options.storage.syncCard({
561
+ orgId: item.orgId,
562
+ id: item.id,
563
+ userId: CARD_SYNC_USER_ID,
564
+ patch: { metadata: {
565
+ labels: after.labels,
566
+ assignees: after.assignees
567
+ } },
568
+ expectedSourceRevision: primarySource(item)?.revision
569
+ });
570
+ } catch (error) {
571
+ if (!(error instanceof WorkItemUpdateConflictError)) throw error;
572
+ }
573
+ return { status: queued === "unchanged" ? "ignored" : "committed" };
574
+ }
421
575
  /** `issues` and `pull_request` actions that are not rule events but still update an existing card. */
422
576
  #cardSyncHandler(parsed) {
423
577
  const action = string(parsed.payload.action);
424
- if (parsed.event === "pull_request") return action === "reopened" ? (...args) => this.#syncReopenedPullRequest(...args) : void 0;
578
+ if (parsed.event === "pull_request") {
579
+ if (action === "reopened") return (...args) => this.#syncReopenedPullRequest(...args);
580
+ return PULL_REQUEST_UPDATE_ACTIONS.has(action ?? "") ? (...args) => this.#pullRequestUpdate(...args) : void 0;
581
+ }
425
582
  if (parsed.event !== "issues") return void 0;
426
583
  switch (action) {
427
584
  case "labeled":
@@ -431,6 +588,24 @@ var GithubRules = class {
431
588
  default: return;
432
589
  }
433
590
  }
591
+ /**
592
+ * An issue with no card changed labels or assignees. If intake turned it away (Not routed), it
593
+ * runs intake again the way a new issue does, so a rule naming the label can now take it.
594
+ */
595
+ async #intakeIfNotRouted(parsed, repositoryId, repositoryName, project, itemNumber, kind = "issue") {
596
+ const installationId = number(object(parsed.payload.installation)?.id);
597
+ const login = string(object(parsed.payload.sender)?.login);
598
+ const scope = {
599
+ orgId: project.orgId,
600
+ factoryProjectId: project.factoryProjectId
601
+ };
602
+ const item = object(kind === "issue" ? parsed.payload.issue : parsed.payload.pull_request);
603
+ if (!installationId || !login || string(item?.state) !== "open" || !await this.options.notRouted?.isNotRouted(scope, legacySourceKey(repositoryId, kind, itemNumber))) return { status: "ignored" };
604
+ const opened = openedByAuthor(parsed, item);
605
+ if (!opened) return { status: "ignored" };
606
+ const event = kind === "issue" ? "issueOpened" : "pullRequestOpened";
607
+ return this.#ingestProject(opened.arrival, event, installationId, repositoryId, repositoryName, opened.login, project);
608
+ }
434
609
  async ingest(parsed) {
435
610
  try {
436
611
  const result = await this.#ingest(parsed);
@@ -454,7 +629,11 @@ var GithubRules = class {
454
629
  }
455
630
  if (!installationId || !repositoryId || !repositoryName || !login) return { status: "ignored" };
456
631
  const cardSync = this.#cardSyncHandler(parsed);
457
- const handle = event ? (project) => this.#ingestProject(parsed, event, installationId, repositoryId, repositoryName, login, project) : cardSync ? (project) => cardSync(parsed, repositoryId, repositoryName, project) : void 0;
632
+ const handle = event ? async (project) => {
633
+ const synced = cardSync ? await cardSync(parsed, repositoryId, repositoryName, project) : void 0;
634
+ const result = await this.#ingestProject(parsed, event, installationId, repositoryId, repositoryName, login, project);
635
+ return result.status === "ignored" && synced?.status === "committed" ? synced : result;
636
+ } : cardSync ? (project) => cardSync(parsed, repositoryId, repositoryName, project) : void 0;
458
637
  if (!handle) return { status: "ignored" };
459
638
  const projects = await this.options.sourceControl.projectRepositories.listByExternalRepository({
460
639
  installationExternalId: String(installationId),
@@ -519,12 +698,19 @@ var GithubRules = class {
519
698
  const pullRequestFactoryAuthored = provenance !== null || this.#isFactoryLogin(pullRequestAuthor);
520
699
  const resolvedItem = await this.#relatedItem(project.orgId, project.factoryProjectId, repositoryId, repositoryName, issueNumber, pullRequestNumber, sameRepositoryHeadBranch(pullRequest, repositoryId), reReviewEvent ? null : provenance, senderIsResponder && !reviewEntryRequested);
521
700
  const matchedItem = reviewEntryRequested && resolvedItem?.externalSource?.type !== "pull-request" && !(resolvedItem && pullRequestNumber && attachedPullRequestSource(resolvedItem, repositoryId, repositoryName, pullRequestNumber)) ? void 0 : resolvedItem;
701
+ const reopenedItem = event === "issueReopened" ? issue : event === "pullRequestReopened" ? pullRequest : void 0;
702
+ if (reopenedItem && !resolvedItem && string(reopenedItem.state) === "open") {
703
+ const opened = openedByAuthor(parsed, reopenedItem);
704
+ if (!opened) return { status: "ignored" };
705
+ return this.#ingestProject(opened.arrival, event === "issueReopened" ? "issueOpened" : "pullRequestOpened", installationId, repositoryId, repositoryName, opened.login, project);
706
+ }
522
707
  const intake = intakeTarget(this.#boards(project));
523
708
  const actor = await githubActor(this.options.github, {
524
709
  installationId,
525
710
  repository: repositoryName,
526
711
  login,
527
- factoryAuthored: !senderIsResponder && provenance !== null || this.#isFactoryLogin(login)
712
+ factoryAuthored: !senderIsResponder && provenance !== null || this.#isFactoryLogin(login),
713
+ human: string(object(parsed.payload.sender)?.type) === "User"
528
714
  });
529
715
  if (actor.type === "github" && actor.factoryAuthored && (event === "issueCommentCreated" || event === "issueCommentEdited") && string(issueComment?.body)?.includes(FACTORY_TRIAGE_COMMENT_MARKER)) return { status: "ignored" };
530
716
  const home = event === "pullRequestOpened" && pullRequestNumber ? await this.#pullRequestHome({
@@ -676,6 +862,10 @@ var GithubRules = class {
676
862
  }) ?? activitySignalFor(context);
677
863
  if (routed) decisions = [{ ...routed }];
678
864
  }
865
+ if (context.pullRequest) {
866
+ const trusted = githubPullRequestTrusted(context);
867
+ decisions = decisions.map((entry) => withPullRequestTrust(entry, trusted));
868
+ }
679
869
  const committed = await this.options.storage.commitRuleEvaluation({
680
870
  orgId: project.orgId,
681
871
  factoryProjectId: project.factoryProjectId,
@@ -847,23 +1037,20 @@ var GithubRules = class {
847
1037
  * Linear, incident.io) carries no repository of its own to match against, so it is taken as it is.
848
1038
  * In order: the card that authored it (on a board that reviews pull requests), by provenance or session branch,
849
1039
  * when the pull request is Factory-authored or its author is trusted; it is filed there. Then,
850
- * for a trusted author only, the card of the first issue its description says it closes; an
851
- * intake route decides whether it joins that one. An untrusted author's pull request never
852
- * lands on another card, whatever it says.
1040
+ * whoever opened it, the card of the first issue its description says it closes; it links to that
1041
+ * one. Linking runs nothing: an outsider's pull request is recorded as untrusted there.
853
1042
  */
854
1043
  async #pullRequestHome(input) {
855
- if (!input.factoryAuthored && !input.authorTrusted) return void 0;
856
1044
  const items = await this.options.storage.listShared({
857
1045
  orgId: input.orgId,
858
1046
  factoryProjectId: input.factoryProjectId
859
1047
  });
860
1048
  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;
861
1049
  const eligible = (card) => card.externalSource != null && card.externalSource.type !== "pull-request" && card.externalSource.integrationId !== "gitlab" && !isTerminalWorkItem(this.#boards(input), card) && (card.externalSource.integrationId !== "github" || cardBelongsToRepository(card, input.repositoryId, input.repositoryName));
862
- if (input.matchedItem && eligible(input.matchedItem) && cardBoardFlow(this.#boards(input), input.matchedItem).pullRequestReview !== void 0) return {
1050
+ if ((input.factoryAuthored || input.authorTrusted) && input.matchedItem && eligible(input.matchedItem) && cardBoardFlow(this.#boards(input), input.matchedItem).pullRequestReview !== void 0) return {
863
1051
  card: input.matchedItem,
864
1052
  reason: "authored"
865
1053
  };
866
- if (!input.authorTrusted) return void 0;
867
1054
  for (const issueNumber of closingIssueNumbers(input.body, {
868
1055
  provider: "github",
869
1056
  repository: input.repositoryName
@@ -1528,6 +1715,7 @@ function githubRulesOptions(github, context) {
1528
1715
  storage: context.runtime.workItems,
1529
1716
  configVersion: context.runtime.configVersion,
1530
1717
  boards: context.runtime.boards,
1718
+ ...context.runtime.notRouted ? { notRouted: context.runtime.notRouted } : {},
1531
1719
  ...context.hooks?.onRepositoryPush ? { onRepositoryPush: context.hooks.onRepositoryPush } : {}
1532
1720
  };
1533
1721
  }
@@ -1542,6 +1730,6 @@ function attachGithubReconciler(github, context, fetchPullRequest) {
1542
1730
  if (!options) return void 0;
1543
1731
  return createGithubPullRequestReconciler(options, fetchPullRequest);
1544
1732
  }
1545
- export { GithubRules, RECONCILE_ERROR_SAMPLE_LIMIT, activitySignalFor, attachGithubPullRequestDiscovery, attachGithubReconciler, attachGithubRules, attachedPullRequestSource, boardEventFor, cardBelongsToRepository, createGithubPullRequestDiscovery, createGithubPullRequestReconciler, githubItemSources, githubRulesOptions, isFactoryGithubLogin, listedPullRequestState, openPullRequestLister, polledPullRequestEvent, reconcilableIssueNumber, reconciledClosedEvent, reconciledIssueClosedEvent, reconciledIssueOpenedEvent, reconciledIssueRelabeledEvent, reconciledPullRequestOpenedEvent, sameStrings, sweepTrustLookup, trustedCollaborator };
1733
+ export { GithubRules, RECONCILE_ERROR_SAMPLE_LIMIT, activitySignalFor, attachGithubPullRequestDiscovery, attachGithubReconciler, attachGithubRules, attachedPullRequestSource, boardEventFor, cardBelongsToRepository, createGithubPullRequestDiscovery, createGithubPullRequestReconciler, githubItemSources, githubPullRequestTrusted, githubRulesOptions, isFactoryGithubLogin, listedPullRequestState, openPullRequestLister, polledPullRequestEvent, reconcilableIssueNumber, reconciledClosedEvent, reconciledIssueClosedEvent, reconciledIssueOpenedEvent, reconciledIssueRelabeledEvent, reconciledPullRequestOpenedEvent, sameStrings, sweepTrustLookup, trustedCollaborator };
1546
1734
 
1547
1735
  //# sourceMappingURL=rules.js.map