@mastra/factory 0.27.0-alpha.0 → 0.27.0-alpha.1

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 (62) hide show
  1. package/dist/integrations/incidentio/default-rules.d.ts +0 -1
  2. package/dist/integrations/incidentio/default-rules.d.ts.map +1 -1
  3. package/dist/integrations/incidentio/default-rules.js +0 -2
  4. package/dist/integrations/incidentio/default-rules.js.map +1 -1
  5. package/dist/integrations/incidentio/rules.d.ts.map +1 -1
  6. package/dist/integrations/incidentio/rules.js +2 -27
  7. package/dist/integrations/incidentio/rules.js.map +1 -1
  8. package/dist/integrations/jira/default-rules.d.ts +0 -1
  9. package/dist/integrations/jira/default-rules.d.ts.map +1 -1
  10. package/dist/integrations/jira/default-rules.js +0 -2
  11. package/dist/integrations/jira/default-rules.js.map +1 -1
  12. package/dist/integrations/jira/routes.d.ts +1 -1
  13. package/dist/integrations/jira/routes.d.ts.map +1 -1
  14. package/dist/integrations/jira/routes.js +6 -3
  15. package/dist/integrations/jira/routes.js.map +1 -1
  16. package/dist/integrations/jira/rules.d.ts.map +1 -1
  17. package/dist/integrations/jira/rules.js +2 -22
  18. package/dist/integrations/jira/rules.js.map +1 -1
  19. package/dist/integrations/linear/default-rules.d.ts +0 -1
  20. package/dist/integrations/linear/default-rules.d.ts.map +1 -1
  21. package/dist/integrations/linear/default-rules.js +0 -2
  22. package/dist/integrations/linear/default-rules.js.map +1 -1
  23. package/dist/integrations/linear/routes.d.ts +1 -1
  24. package/dist/integrations/linear/routes.d.ts.map +1 -1
  25. package/dist/integrations/linear/routes.js +13 -15
  26. package/dist/integrations/linear/routes.js.map +1 -1
  27. package/dist/integrations/linear/rules.d.ts.map +1 -1
  28. package/dist/integrations/linear/rules.js +2 -23
  29. package/dist/integrations/linear/rules.js.map +1 -1
  30. package/dist/integrations/platform/jira/routes.d.ts +1 -1
  31. package/dist/integrations/platform/jira/routes.d.ts.map +1 -1
  32. package/dist/integrations/platform/jira/routes.js +6 -3
  33. package/dist/integrations/platform/jira/routes.js.map +1 -1
  34. package/dist/rules/dispatcher.d.ts.map +1 -1
  35. package/dist/rules/dispatcher.js +24 -45
  36. package/dist/rules/dispatcher.js.map +1 -1
  37. package/dist/rules/processor.d.ts.map +1 -1
  38. package/dist/rules/processor.js +1 -0
  39. package/dist/rules/processor.js.map +1 -1
  40. package/dist/rules/types.d.ts +0 -7
  41. package/dist/rules/types.d.ts.map +1 -1
  42. package/dist/rules/types.js.map +1 -1
  43. package/dist/rules/validation.d.ts.map +1 -1
  44. package/dist/rules/validation.js +0 -2
  45. package/dist/rules/validation.js.map +1 -1
  46. package/dist/storage/domains/work-items/base.d.ts +0 -34
  47. package/dist/storage/domains/work-items/base.d.ts.map +1 -1
  48. package/dist/storage/domains/work-items/base.js +10 -139
  49. package/dist/storage/domains/work-items/base.js.map +1 -1
  50. package/package.json +7 -7
  51. package/dist/integrations/incidentio/claim.d.ts +0 -8
  52. package/dist/integrations/incidentio/claim.d.ts.map +0 -1
  53. package/dist/integrations/incidentio/claim.js +0 -13
  54. package/dist/integrations/incidentio/claim.js.map +0 -1
  55. package/dist/integrations/jira/claim.d.ts +0 -21
  56. package/dist/integrations/jira/claim.d.ts.map +0 -1
  57. package/dist/integrations/jira/claim.js +0 -37
  58. package/dist/integrations/jira/claim.js.map +0 -1
  59. package/dist/integrations/linear/claim.d.ts +0 -8
  60. package/dist/integrations/linear/claim.d.ts.map +0 -1
  61. package/dist/integrations/linear/claim.js +0 -13
  62. package/dist/integrations/linear/claim.js.map +0 -1
@@ -1,10 +1,10 @@
1
1
  import { FACTORY_RULE_STAGES, externalSourceForWorkItem, externallyAuthoredWorkItem } from "./types.js";
2
2
  import { assertFactoryDecisionTarget, validateFactoryRuleDecision } from "./validation.js";
3
- import { boardForWorkItem, resolvePhaseSemantics, workItemPhaseSemantics } from "../boards/semantics.js";
3
+ import { boardForWorkItem, isTerminalWorkItem, resolvePhaseSemantics, workItemPhaseSemantics } from "../boards/semantics.js";
4
4
  import "../boards/index.js";
5
5
  import { toBoardSource } from "../boards/project-boards.js";
6
6
  import { FactoryDispatchError, factoryDispatchFailureCode, factoryDispatchFailureMetadata } from "./dispatch-errors.js";
7
- import { FACTORY_RULE_MATERIALIZATION_KEY, WorkItemClaimConflictError } from "../storage/domains/work-items/base.js";
7
+ import { FACTORY_RULE_MATERIALIZATION_KEY } from "../storage/domains/work-items/base.js";
8
8
  import { moveCardToBoard } from "../boards/relocate.js";
9
9
  import { decisionOvertaken } from "./decision-applicability.js";
10
10
  import { FACTORY_OPEN_RUN_STALE_MS, heartbeatSessionOpenRun, isOpenRunLiveElsewhere, listSessionOpenRuns, recordSessionRunStart, waitForSessionRunAudit } from "../session/run-audit.js";
@@ -833,6 +833,19 @@ var FactoryDecisionDispatcher = class {
833
833
  }
834
834
  }
835
835
  }
836
+ /**
837
+ * The project's card for a Linear issue by its stable id, wherever its identifier now points.
838
+ * A live card wins over finished ones, as in the Linear rules and issue detail route.
839
+ */
840
+ async #linearCardByIssueId(record, decision, boards) {
841
+ const issueId = decision.metadata?.linearIssueId;
842
+ if (decision.source !== "linear-issue" || typeof issueId !== "string") return null;
843
+ const cards = (await this.#storage.list({
844
+ orgId: record.orgId,
845
+ factoryProjectId: record.factoryProjectId
846
+ })).filter((item) => item.externalSource?.integrationId === "linear" && item.metadata?.linearIssueId === issueId);
847
+ return cards.find((item) => !isTerminalWorkItem(boards, item)) ?? cards[0] ?? null;
848
+ }
836
849
  async #upsertLinkedItem(record, decision, causalChain) {
837
850
  if (this.#factoryProjectExists && !await this.#factoryProjectExists({
838
851
  orgId: record.orgId,
@@ -862,13 +875,20 @@ var FactoryDecisionDispatcher = class {
862
875
  factoryProjectId: record.factoryProjectId,
863
876
  decision
864
877
  }) ?? null;
865
- const upsert = () => this.#storage.upsert({
878
+ const renamed = existing ? null : await this.#linearCardByIssueId(record, decision, boards);
879
+ let result = renamed ? {
880
+ created: false,
881
+ item: renamed,
882
+ previous: {
883
+ stages: renamed.stages,
884
+ sessionRoles: Object.keys(renamed.sessions)
885
+ }
886
+ } : await this.#storage.upsert({
866
887
  orgId: record.orgId,
867
888
  userId: "factory-rule-dispatcher",
868
889
  factoryProjectId: record.factoryProjectId,
869
890
  input: {
870
891
  externalSource: externalSourceForDecision(decision),
871
- ...decision.claimKey ? { claimKey: decision.claimKey } : {},
872
892
  parentWorkItemId,
873
893
  title: decision.title,
874
894
  board: decision.board,
@@ -881,29 +901,6 @@ var FactoryDecisionDispatcher = class {
881
901
  },
882
902
  reuseMode: "preserve"
883
903
  });
884
- let result;
885
- try {
886
- result = await upsert().catch(async (error) => {
887
- if (error instanceof WorkItemClaimConflictError && await this.#releaseDeletedProjectClaim(record.orgId, decision.claimKey)) return upsert();
888
- throw error;
889
- });
890
- } catch (error) {
891
- if (error instanceof WorkItemClaimConflictError) return;
892
- throw error;
893
- }
894
- const { claimKey } = decision;
895
- if (!result.created && claimKey && !result.item.claimKey) {
896
- const claim = {
897
- orgId: record.orgId,
898
- id: result.item.id,
899
- claimKey
900
- };
901
- const claimed = await this.#storage.claimWorkItem(claim) ?? (await this.#releaseDeletedProjectClaim(record.orgId, claimKey) ? await this.#storage.claimWorkItem(claim) : null);
902
- if (claimed) result = {
903
- ...result,
904
- item: claimed
905
- };
906
- }
907
904
  const itemBoard = boardForWorkItem(result.item);
908
905
  if (!skipRules && itemBoard !== decision.board) throw new Error(`The work item belongs to board "${itemBoard}", not "${decision.board}".`);
909
906
  if (!result.item.parentWorkItemId && parentWorkItemId && parentWorkItemId !== result.item.id) {
@@ -996,24 +993,6 @@ var FactoryDecisionDispatcher = class {
996
993
  });
997
994
  if (moved.status === "rejected") throw new Error(`${moved.code}: ${moved.reason}`);
998
995
  }
999
- /** Drops `claimKey` when the card holding it belongs to a deleted Factory project; true when it is free again. */
1000
- async #releaseDeletedProjectClaim(orgId, claimKey) {
1001
- if (!claimKey || !this.#factoryProjectExists) return false;
1002
- const holder = await this.#storage.getByClaimKey({
1003
- orgId,
1004
- claimKey
1005
- });
1006
- if (!holder) return true;
1007
- if (await this.#factoryProjectExists({
1008
- orgId,
1009
- factoryProjectId: holder.factoryProjectId
1010
- })) return false;
1011
- await this.#storage.releaseProjectClaims({
1012
- orgId,
1013
- factoryProjectId: holder.factoryProjectId
1014
- });
1015
- return true;
1016
- }
1017
996
  async #requireItem(record) {
1018
997
  if (!record.workItemId) throw new Error("Factory decision is not linked to a work item.");
1019
998
  const item = await this.#storage.get({