@mastra/factory 0.12.0-alpha.1 → 0.12.0-alpha.10

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 (140) hide show
  1. package/dist/factory.d.ts.map +1 -1
  2. package/dist/factory.js +31 -14
  3. package/dist/factory.js.map +1 -1
  4. package/dist/integrations/base.d.ts +9 -0
  5. package/dist/integrations/base.d.ts.map +1 -1
  6. package/dist/integrations/github/commits.d.ts +29 -0
  7. package/dist/integrations/github/commits.d.ts.map +1 -0
  8. package/dist/integrations/github/commits.js +29 -0
  9. package/dist/integrations/github/commits.js.map +1 -0
  10. package/dist/integrations/github/integration.d.ts.map +1 -1
  11. package/dist/integrations/github/integration.js +47 -10
  12. package/dist/integrations/github/integration.js.map +1 -1
  13. package/dist/integrations/github/issue-reconciler.d.ts.map +1 -1
  14. package/dist/integrations/github/issue-reconciler.js +22 -14
  15. package/dist/integrations/github/issue-reconciler.js.map +1 -1
  16. package/dist/integrations/github/reconcile-worker.d.ts.map +1 -1
  17. package/dist/integrations/github/reconcile-worker.js +2 -2
  18. package/dist/integrations/github/reconcile-worker.js.map +1 -1
  19. package/dist/integrations/github/routes.d.ts.map +1 -1
  20. package/dist/integrations/github/routes.js +35 -0
  21. package/dist/integrations/github/routes.js.map +1 -1
  22. package/dist/integrations/github/rules.d.ts +6 -0
  23. package/dist/integrations/github/rules.d.ts.map +1 -1
  24. package/dist/integrations/github/rules.js +58 -12
  25. package/dist/integrations/github/rules.js.map +1 -1
  26. package/dist/integrations/github/settle-or-abort.d.ts +7 -0
  27. package/dist/integrations/github/settle-or-abort.d.ts.map +1 -0
  28. package/dist/integrations/github/settle-or-abort.js +25 -0
  29. package/dist/integrations/github/settle-or-abort.js.map +1 -0
  30. package/dist/integrations/platform/github/event-worker.d.ts.map +1 -1
  31. package/dist/integrations/platform/github/event-worker.js +3 -3
  32. package/dist/integrations/platform/github/event-worker.js.map +1 -1
  33. package/dist/integrations/platform/github/integration.d.ts.map +1 -1
  34. package/dist/integrations/platform/github/integration.js +37 -5
  35. package/dist/integrations/platform/github/integration.js.map +1 -1
  36. package/dist/integrations/slack/feed-publisher.d.ts +26 -0
  37. package/dist/integrations/slack/feed-publisher.d.ts.map +1 -0
  38. package/dist/integrations/slack/feed-publisher.js +44 -0
  39. package/dist/integrations/slack/feed-publisher.js.map +1 -0
  40. package/dist/integrations/slack/integration.d.ts +2 -0
  41. package/dist/integrations/slack/integration.d.ts.map +1 -1
  42. package/dist/integrations/slack/integration.js +5 -0
  43. package/dist/integrations/slack/integration.js.map +1 -1
  44. package/dist/integrations/slack/slack.d.ts +13 -1
  45. package/dist/integrations/slack/slack.d.ts.map +1 -1
  46. package/dist/integrations/slack/slack.js +78 -6
  47. package/dist/integrations/slack/slack.js.map +1 -1
  48. package/dist/routes/projects.d.ts.map +1 -1
  49. package/dist/routes/projects.js +4 -0
  50. package/dist/routes/projects.js.map +1 -1
  51. package/dist/routes/skills.d.ts.map +1 -1
  52. package/dist/routes/skills.js +9 -3
  53. package/dist/routes/skills.js.map +1 -1
  54. package/dist/routes/surface.d.ts +5 -0
  55. package/dist/routes/surface.d.ts.map +1 -1
  56. package/dist/routes/surface.js +34 -6
  57. package/dist/routes/surface.js.map +1 -1
  58. package/dist/routes/tenant-credentials.d.ts +4 -4
  59. package/dist/routes/tenant-credentials.d.ts.map +1 -1
  60. package/dist/routes/tenant-credentials.js +22 -7
  61. package/dist/routes/tenant-credentials.js.map +1 -1
  62. package/dist/routes/work-items.d.ts.map +1 -1
  63. package/dist/routes/work-items.js +12 -1
  64. package/dist/routes/work-items.js.map +1 -1
  65. package/dist/rules/defaults.d.ts.map +1 -1
  66. package/dist/rules/defaults.js +20 -8
  67. package/dist/rules/defaults.js.map +1 -1
  68. package/dist/rules/dispatch-errors.d.ts.map +1 -1
  69. package/dist/rules/dispatch-errors.js +8 -0
  70. package/dist/rules/dispatch-errors.js.map +1 -1
  71. package/dist/rules/dispatcher.d.ts +5 -0
  72. package/dist/rules/dispatcher.d.ts.map +1 -1
  73. package/dist/rules/dispatcher.js +125 -62
  74. package/dist/rules/dispatcher.js.map +1 -1
  75. package/dist/rules/processor.d.ts.map +1 -1
  76. package/dist/rules/processor.js +2 -3
  77. package/dist/rules/processor.js.map +1 -1
  78. package/dist/rules/start-coordinator.d.ts +2 -0
  79. package/dist/rules/start-coordinator.d.ts.map +1 -1
  80. package/dist/rules/start-coordinator.js +6 -1
  81. package/dist/rules/start-coordinator.js.map +1 -1
  82. package/dist/rules/transition-service.d.ts +2 -2
  83. package/dist/rules/transition-service.d.ts.map +1 -1
  84. package/dist/rules/transition-service.js +33 -12
  85. package/dist/rules/transition-service.js.map +1 -1
  86. package/dist/rules/types.d.ts +27 -1
  87. package/dist/rules/types.d.ts.map +1 -1
  88. package/dist/rules/types.js +37 -1
  89. package/dist/rules/types.js.map +1 -1
  90. package/dist/rules/validation.d.ts.map +1 -1
  91. package/dist/rules/validation.js +3 -1
  92. package/dist/rules/validation.js.map +1 -1
  93. package/dist/sandbox/session-sandbox.d.ts.map +1 -1
  94. package/dist/sandbox/session-sandbox.js +2 -2
  95. package/dist/sandbox/session-sandbox.js.map +1 -1
  96. package/dist/sandbox/workdir.d.ts +7 -2
  97. package/dist/sandbox/workdir.d.ts.map +1 -1
  98. package/dist/sandbox/workdir.js +11 -4
  99. package/dist/sandbox/workdir.js.map +1 -1
  100. package/dist/skills/catalog.d.ts +4 -1
  101. package/dist/skills/catalog.d.ts.map +1 -1
  102. package/dist/skills/catalog.js +11 -4
  103. package/dist/skills/catalog.js.map +1 -1
  104. package/dist/skills/service.d.ts +2 -1
  105. package/dist/skills/service.d.ts.map +1 -1
  106. package/dist/skills/service.js +5 -2
  107. package/dist/skills/service.js.map +1 -1
  108. package/dist/storage/domains/comments/base.d.ts.map +1 -1
  109. package/dist/storage/domains/comments/base.js +2 -4
  110. package/dist/storage/domains/comments/base.js.map +1 -1
  111. package/dist/storage/domains/comments/domain.d.ts +9 -2
  112. package/dist/storage/domains/comments/domain.d.ts.map +1 -1
  113. package/dist/storage/domains/comments/domain.js +18 -15
  114. package/dist/storage/domains/comments/domain.js.map +1 -1
  115. package/dist/storage/domains/comments/feed-context.d.ts +2 -0
  116. package/dist/storage/domains/comments/feed-context.d.ts.map +1 -1
  117. package/dist/storage/domains/comments/feed-context.js +6 -4
  118. package/dist/storage/domains/comments/feed-context.js.map +1 -1
  119. package/dist/storage/domains/comments/feed-sync.d.ts +9 -26
  120. package/dist/storage/domains/comments/feed-sync.d.ts.map +1 -1
  121. package/dist/storage/domains/projects/base.d.ts +3 -0
  122. package/dist/storage/domains/projects/base.d.ts.map +1 -1
  123. package/dist/storage/domains/projects/base.js +7 -0
  124. package/dist/storage/domains/projects/base.js.map +1 -1
  125. package/dist/storage/domains/source-control/inmemory.d.ts.map +1 -1
  126. package/dist/storage/domains/source-control/inmemory.js +5 -1
  127. package/dist/storage/domains/source-control/inmemory.js.map +1 -1
  128. package/dist/storage/domains/work-items/base.d.ts +32 -3
  129. package/dist/storage/domains/work-items/base.d.ts.map +1 -1
  130. package/dist/storage/domains/work-items/base.js +100 -51
  131. package/dist/storage/domains/work-items/base.js.map +1 -1
  132. package/dist/workspace.d.ts +20 -2
  133. package/dist/workspace.d.ts.map +1 -1
  134. package/dist/workspace.js +49 -15
  135. package/dist/workspace.js.map +1 -1
  136. package/factory-skills/factory-rereview/SKILL.md +13 -12
  137. package/factory-skills/factory-review/SKILL.md +13 -11
  138. package/factory-skills/factory-triage/SKILL.md +27 -6
  139. package/package.json +9 -10
  140. package/CHANGELOG.md +0 -3359
@@ -43,6 +43,8 @@ const FACTORY_DISPATCH_FAILURE_CODES = [
43
43
  "source_repository_missing",
44
44
  "unsupported_provider_item",
45
45
  "notification_delivery_failed",
46
+ "plan_awaiting_approval",
47
+ "run_awaiting_input",
46
48
  "repository_git_missing",
47
49
  "repository_egress_blocked",
48
50
  "repository_clone_failed",
@@ -127,6 +129,10 @@ const WORK_ITEMS_SCHEMA = {
127
129
  type: "timestamp",
128
130
  nullable: true
129
131
  },
132
+ plans_preapproved_at: {
133
+ type: "timestamp",
134
+ nullable: true
135
+ },
130
136
  comment_count: {
131
137
  type: "integer",
132
138
  default: 0
@@ -147,25 +153,39 @@ const WORK_ITEMS_SCHEMA = {
147
153
  name: "work_items_project_source_key_unique",
148
154
  columns: ["factory_project_id", "source_key"]
149
155
  }],
150
- indexes: [{
151
- name: "work_items_org_project_created_at_id_idx",
152
- columns: [
153
- "org_id",
154
- "factory_project_id",
155
- "created_at",
156
- "id"
157
- ]
158
- }, {
159
- name: "work_items_project_parent_idx",
160
- columns: [
161
- "org_id",
162
- "factory_project_id",
163
- "parent_work_item_id"
164
- ]
165
- }]
156
+ indexes: [
157
+ {
158
+ name: "work_items_org_project_created_at_id_idx",
159
+ columns: [
160
+ "org_id",
161
+ "factory_project_id",
162
+ "created_at",
163
+ "id"
164
+ ]
165
+ },
166
+ {
167
+ name: "work_items_project_parent_idx",
168
+ columns: [
169
+ "org_id",
170
+ "factory_project_id",
171
+ "parent_work_item_id"
172
+ ]
173
+ },
174
+ {
175
+ name: "work_items_source_key_idx",
176
+ columns: ["source_key"]
177
+ }
178
+ ]
166
179
  };
167
- function sourceKey(source) {
168
- return source ? `${source.integrationId}:${source.type}:${source.externalId}` : null;
180
+ /**
181
+ * The one shape a platform id takes in a lookup key, for cards and for the
182
+ * comments mirrored off them alike: whoever writes a second builder reopens the
183
+ * cross-workspace collision the `workspaceId` is here to close.
184
+ */
185
+ function externalSourceKey(source) {
186
+ if (!source) return null;
187
+ const workspace = source.workspaceId ? `${source.workspaceId}:` : "";
188
+ return `${source.integrationId}:${source.type}:${workspace}${source.externalId}`;
169
189
  }
170
190
  function toWorkItem(row) {
171
191
  return {
@@ -181,6 +201,7 @@ function toWorkItem(row) {
181
201
  metadata: row.metadata,
182
202
  triageType: row.triage_type ?? null,
183
203
  autonomyArmedAt: row.autonomy_armed_at ?? null,
204
+ plansPreapprovedAt: row.plans_preapproved_at ?? null,
184
205
  commentCount: row.comment_count ?? 0,
185
206
  feedActivityAt: row.feed_activity_at ?? null,
186
207
  revision: row.revision,
@@ -199,7 +220,7 @@ function patchColumns(changes) {
199
220
  ...changes.sessions !== void 0 ? { sessions: changes.sessions } : {},
200
221
  ...changes.metadata !== void 0 ? { metadata: changes.metadata } : {},
201
222
  ...changes.triageType !== void 0 ? { triage_type: changes.triageType } : {},
202
- ...changes.autonomyArmedAt !== void 0 && changes.autonomyArmedAt !== null ? { autonomy_armed_at: changes.autonomyArmedAt } : {},
223
+ ...changes.autonomyArmedAt !== void 0 ? { autonomy_armed_at: changes.autonomyArmedAt } : {},
203
224
  ...changes.revision !== void 0 ? { revision: changes.revision } : {},
204
225
  ...changes.updatedAt !== void 0 ? { updated_at: changes.updatedAt } : {}
205
226
  };
@@ -585,6 +606,10 @@ const FACTORY_GOVERNANCE_SCHEMAS = [
585
606
  type: "text",
586
607
  nullable: true
587
608
  },
609
+ failure_code: {
610
+ type: "text",
611
+ nullable: true
612
+ },
588
613
  completed_at: {
589
614
  type: "timestamp",
590
615
  nullable: true
@@ -697,6 +722,7 @@ function toPendingStart(row) {
697
722
  leaseOwner: row.lease_owner ?? null,
698
723
  leaseExpiresAt: row.lease_expires_at ?? null,
699
724
  lastError: row.last_error ?? null,
725
+ failureCode: row.failure_code ?? null,
700
726
  completedAt: row.completed_at ?? null,
701
727
  createdAt: row.created_at,
702
728
  updatedAt: row.updated_at
@@ -830,7 +856,7 @@ var WorkItemsStorage = class extends FactoryStorageDomain {
830
856
  lease_owner: null,
831
857
  lease_expires_at: null,
832
858
  last_error: input.lastError,
833
- ...table === "factory_deferred_decisions" ? { failure_code: input.failureCode } : {},
859
+ failure_code: input.failureCode,
834
860
  completed_at: input.terminal ? input.now : null,
835
861
  ...table === "factory_deferred_decisions" && input.terminal ? { failure_occurrence: Number(current.failure_occurrence ?? 0) + 1 } : {},
836
862
  updated_at: input.now
@@ -891,6 +917,16 @@ var WorkItemsStorage = class extends FactoryStorageDomain {
891
917
  });
892
918
  return row ? toWorkItem(row) : null;
893
919
  }
920
+ /**
921
+ * Resolve the card a platform thread created, given only its external source.
922
+ * Bare of org/project because an inbound platform message carries neither: an
923
+ * unlinked sender has no tenant. Tenant safety rides on the key's
924
+ * `workspaceId` instead. Ambiguous matches resolve to nothing, not a guess.
925
+ */
926
+ async getBySource(source) {
927
+ const rows = await this.#db.findMany("work_items", { source_key: externalSourceKey(source) });
928
+ return rows.length === 1 ? toWorkItem(rows[0]) : null;
929
+ }
894
930
  async getForProject(orgId, factoryProjectId, id) {
895
931
  const row = await this.#db.findOne("work_items", {
896
932
  id,
@@ -940,19 +976,22 @@ var WorkItemsStorage = class extends FactoryStorageDomain {
940
976
  reason = input.evaluation.reason;
941
977
  return null;
942
978
  }
943
- const arm = input.armAutonomy === true && !existing.autonomyArmedAt;
979
+ const arm = input.autonomy === "arm" && !existing.autonomyArmedAt;
980
+ const disarm = input.autonomy === "disarm" && existing.autonomyArmedAt !== null;
944
981
  const triageType = existing.triageType ?? input.triageType ?? null;
945
982
  const classified = triageType !== existing.triageType;
946
- if (existing.stages.length === 1 && existing.stages[0] === input.destinationStage) return arm || classified ? patchColumns({
983
+ if (existing.stages.length === 1 && existing.stages[0] === input.destinationStage) return arm || disarm || classified ? patchColumns({
947
984
  ...arm ? { autonomyArmedAt: now } : {},
948
- ...classified ? { triageType } : {},
985
+ ...disarm ? { autonomyArmedAt: null } : {},
949
986
  ...classified ? {
987
+ triageType,
950
988
  revision: existing.revision + 1,
951
989
  updatedAt: now
952
990
  } : {}
953
991
  }) : null;
954
992
  return patchColumns({
955
993
  ...arm ? { autonomyArmedAt: now } : {},
994
+ ...disarm ? { autonomyArmedAt: null } : {},
956
995
  ...classified ? { triageType } : {},
957
996
  stages: [input.destinationStage],
958
997
  stageHistory: applyStageTransition(existing.stageHistory, existing.stages, [input.destinationStage], input.actorId, now),
@@ -1001,29 +1040,34 @@ var WorkItemsStorage = class extends FactoryStorageDomain {
1001
1040
  causal_chain: input.causalChain,
1002
1041
  created_at: now
1003
1042
  });
1004
- if (outcome === "accepted" && input.evaluation.outcome === "accepted") for (const [index, decision] of input.evaluation.decisions.entries()) await ops.insertOne("factory_deferred_decisions", {
1005
- org_id: input.orgId,
1006
- factory_project_id: input.factoryProjectId,
1007
- evaluation_id: evaluation.id,
1008
- work_item_id: item.id,
1009
- source_key: decisionSourceKey(decision),
1010
- idempotency_key: String(decision.idempotencyKey),
1011
- effect_ordinal: index,
1012
- effect_hash: factoryDecisionHash(decision),
1013
- causal_chain: input.causalChain,
1014
- actor: null,
1015
- decision,
1016
- status: "pending",
1017
- attempts: 0,
1018
- delivery_generation: 0,
1019
- available_at: now,
1020
- lease_owner: null,
1021
- lease_expires_at: null,
1022
- last_error: null,
1023
- completed_at: null,
1024
- created_at: new Date(now.getTime() + index),
1025
- updated_at: now
1026
- });
1043
+ if (outcome === "accepted" && input.evaluation.outcome === "accepted") for (const [index, decision] of input.evaluation.decisions.entries()) {
1044
+ const approvedBy = decision.type === "invokeSkill" ? input.consentedBy ?? null : null;
1045
+ await ops.insertOne("factory_deferred_decisions", {
1046
+ org_id: input.orgId,
1047
+ factory_project_id: input.factoryProjectId,
1048
+ evaluation_id: evaluation.id,
1049
+ work_item_id: item.id,
1050
+ source_key: decisionSourceKey(decision),
1051
+ idempotency_key: String(decision.idempotencyKey),
1052
+ effect_ordinal: index,
1053
+ effect_hash: factoryDecisionHash(decision),
1054
+ causal_chain: input.causalChain,
1055
+ actor: null,
1056
+ decision,
1057
+ status: "pending",
1058
+ attempts: 0,
1059
+ delivery_generation: 0,
1060
+ available_at: now,
1061
+ lease_owner: null,
1062
+ lease_expires_at: null,
1063
+ last_error: null,
1064
+ approved_at: approvedBy ? now : null,
1065
+ approved_by: approvedBy,
1066
+ completed_at: null,
1067
+ created_at: new Date(now.getTime() + index),
1068
+ updated_at: now
1069
+ });
1070
+ }
1027
1071
  }
1028
1072
  return {
1029
1073
  status: "committed",
@@ -1809,10 +1853,10 @@ var WorkItemsStorage = class extends FactoryStorageDomain {
1809
1853
  id: input.workItem.id,
1810
1854
  org_id: input.orgId,
1811
1855
  factory_project_id: input.factoryProjectId
1812
- }) : sourceKey(create.externalSource) ? await ops.findOne("work_items", {
1856
+ }) : externalSourceKey(create.externalSource) ? await ops.findOne("work_items", {
1813
1857
  org_id: input.orgId,
1814
1858
  factory_project_id: input.factoryProjectId,
1815
- source_key: sourceKey(create.externalSource)
1859
+ source_key: externalSourceKey(create.externalSource)
1816
1860
  }) : null;
1817
1861
  let item;
1818
1862
  if (row) {
@@ -1834,7 +1878,7 @@ var WorkItemsStorage = class extends FactoryStorageDomain {
1834
1878
  created_by: input.userId,
1835
1879
  factory_project_id: input.factoryProjectId,
1836
1880
  external_source: create.externalSource ?? null,
1837
- source_key: sourceKey(create.externalSource),
1881
+ source_key: externalSourceKey(create.externalSource),
1838
1882
  parent_work_item_id: create.parentWorkItemId ?? null,
1839
1883
  title: create.title,
1840
1884
  stages: create.stages ?? [],
@@ -1851,6 +1895,10 @@ var WorkItemsStorage = class extends FactoryStorageDomain {
1851
1895
  const armedRow = await ops.updateAtomic("work_items", { id: item.id }, (current) => current.autonomy_armed_at ? null : { autonomy_armed_at: now });
1852
1896
  if (armedRow) item = toRow(armedRow);
1853
1897
  }
1898
+ if (input.preapprovePlans && !item.plansPreapprovedAt) {
1899
+ const grantedRow = await ops.updateAtomic("work_items", { id: item.id }, (current) => current.plans_preapproved_at ? null : { plans_preapproved_at: now });
1900
+ if (grantedRow) item = toRow(grantedRow);
1901
+ }
1854
1902
  await ops.updateMany("factory_run_bindings", {
1855
1903
  org_id: input.orgId,
1856
1904
  factory_project_id: input.factoryProjectId,
@@ -1897,6 +1945,7 @@ var WorkItemsStorage = class extends FactoryStorageDomain {
1897
1945
  lease_owner: null,
1898
1946
  lease_expires_at: null,
1899
1947
  last_error: null,
1948
+ failure_code: null,
1900
1949
  completed_at: null,
1901
1950
  created_at: now,
1902
1951
  updated_at: now
@@ -1944,7 +1993,7 @@ var WorkItemsStorage = class extends FactoryStorageDomain {
1944
1993
  }
1945
1994
  }
1946
1995
  async #upsert({ orgId, userId, factoryProjectId, input, reuseMode = "update" }, ops) {
1947
- const key = sourceKey(input.externalSource);
1996
+ const key = externalSourceKey(input.externalSource);
1948
1997
  const reuse = async () => {
1949
1998
  if (!key) return null;
1950
1999
  const existing = await ops.findOne("work_items", {
@@ -2189,6 +2238,6 @@ var WorkItemsStorage = class extends FactoryStorageDomain {
2189
2238
  }
2190
2239
  };
2191
2240
  //#endregion
2192
- export { FACTORY_PULL_REQUEST_RECONCILIATION_KEY, FACTORY_RULE_MATERIALIZATION_KEY, WORK_ITEMS_SCHEMA, WorkItemRelationError, WorkItemsStorage, applyStageTransition, factoryActivityAttentionIdentity, factoryAttentionKey, factoryDecisionAttentionIdentity, factoryDecisionHash, factoryMentionAttentionIdentity, isAgentActor, stampSessions, validateParentRelation };
2241
+ export { FACTORY_PULL_REQUEST_RECONCILIATION_KEY, FACTORY_RULE_MATERIALIZATION_KEY, WORK_ITEMS_SCHEMA, WorkItemRelationError, WorkItemsStorage, applyStageTransition, externalSourceKey, factoryActivityAttentionIdentity, factoryAttentionKey, factoryDecisionAttentionIdentity, factoryDecisionHash, factoryMentionAttentionIdentity, isAgentActor, stampSessions, validateParentRelation };
2193
2242
 
2194
2243
  //# sourceMappingURL=base.js.map