@mastra/factory 0.2.2-alpha.0 → 0.2.2-alpha.2

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 (174) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/dist/auth.d.ts +2 -2
  3. package/dist/auth.d.ts.map +1 -1
  4. package/dist/auth.js +401 -314
  5. package/dist/auth.js.map +1 -1
  6. package/dist/capabilities/connection.js +1 -1
  7. package/dist/capabilities/intake.js +1 -1
  8. package/dist/capabilities/version-control.js +1 -1
  9. package/dist/factory.d.ts.map +1 -1
  10. package/dist/factory.js +441 -39455
  11. package/dist/factory.js.map +1 -1
  12. package/dist/index.js +2 -39466
  13. package/dist/integrations/base.js +1 -1
  14. package/dist/integrations/github/config.js +36 -22
  15. package/dist/integrations/github/config.js.map +1 -1
  16. package/dist/integrations/github/factory-session.js +33 -37
  17. package/dist/integrations/github/factory-session.js.map +1 -1
  18. package/dist/integrations/github/integration.js +1037 -3624
  19. package/dist/integrations/github/integration.js.map +1 -1
  20. package/dist/integrations/github/issue-triage.js +71 -71
  21. package/dist/integrations/github/issue-triage.js.map +1 -1
  22. package/dist/integrations/github/pat.js +47 -36
  23. package/dist/integrations/github/pat.js.map +1 -1
  24. package/dist/integrations/github/provenance.js +72 -101
  25. package/dist/integrations/github/provenance.js.map +1 -1
  26. package/dist/integrations/github/routes.d.ts.map +1 -1
  27. package/dist/integrations/github/routes.js +1272 -1833
  28. package/dist/integrations/github/routes.js.map +1 -1
  29. package/dist/integrations/github/rules.js +203 -471
  30. package/dist/integrations/github/rules.js.map +1 -1
  31. package/dist/integrations/github/sandbox.d.ts +2 -0
  32. package/dist/integrations/github/sandbox.d.ts.map +1 -1
  33. package/dist/integrations/github/sandbox.js +401 -326
  34. package/dist/integrations/github/sandbox.js.map +1 -1
  35. package/dist/integrations/github/session-subscriptions.js +165 -223
  36. package/dist/integrations/github/session-subscriptions.js.map +1 -1
  37. package/dist/integrations/github/subscriptions.js +43 -51
  38. package/dist/integrations/github/subscriptions.js.map +1 -1
  39. package/dist/integrations/github/token-refresh.js +15 -18
  40. package/dist/integrations/github/token-refresh.js.map +1 -1
  41. package/dist/integrations/github/webhook.js +359 -343
  42. package/dist/integrations/github/webhook.js.map +1 -1
  43. package/dist/integrations/linear/agent-tools.js +77 -100
  44. package/dist/integrations/linear/agent-tools.js.map +1 -1
  45. package/dist/integrations/linear/integration.js +611 -1309
  46. package/dist/integrations/linear/integration.js.map +1 -1
  47. package/dist/integrations/linear/routes.js +227 -230
  48. package/dist/integrations/linear/routes.js.map +1 -1
  49. package/dist/integrations/linear/rules.js +126 -346
  50. package/dist/integrations/linear/rules.js.map +1 -1
  51. package/dist/integrations/linear/storage.js +1 -1
  52. package/dist/integrations/platform/api-client.js +131 -135
  53. package/dist/integrations/platform/api-client.js.map +1 -1
  54. package/dist/integrations/platform/github/event-worker.js +275 -573
  55. package/dist/integrations/platform/github/event-worker.js.map +1 -1
  56. package/dist/integrations/platform/github/integration.d.ts.map +1 -1
  57. package/dist/integrations/platform/github/integration.js +845 -3942
  58. package/dist/integrations/platform/github/integration.js.map +1 -1
  59. package/dist/integrations/platform/linear/integration.js +457 -1309
  60. package/dist/integrations/platform/linear/integration.js.map +1 -1
  61. package/dist/integrations/workos/integration.js +80 -85
  62. package/dist/integrations/workos/integration.js.map +1 -1
  63. package/dist/routes/config.d.ts.map +1 -1
  64. package/dist/routes/config.js +933 -915
  65. package/dist/routes/config.js.map +1 -1
  66. package/dist/routes/custom-provider-source.js +110 -93
  67. package/dist/routes/custom-provider-source.js.map +1 -1
  68. package/dist/routes/fs.js +401 -349
  69. package/dist/routes/fs.js.map +1 -1
  70. package/dist/routes/intake.js +165 -165
  71. package/dist/routes/intake.js.map +1 -1
  72. package/dist/routes/oauth.js +447 -25166
  73. package/dist/routes/oauth.js.map +1 -1
  74. package/dist/routes/projects.js +437 -376
  75. package/dist/routes/projects.js.map +1 -1
  76. package/dist/routes/provider-credentials.js +76 -58
  77. package/dist/routes/provider-credentials.js.map +1 -1
  78. package/dist/routes/route.js +12 -8
  79. package/dist/routes/route.js.map +1 -1
  80. package/dist/routes/skills.js +160 -173
  81. package/dist/routes/skills.js.map +1 -1
  82. package/dist/routes/surface.js +250 -28618
  83. package/dist/routes/surface.js.map +1 -1
  84. package/dist/routes/tenant-credentials.js +141 -166
  85. package/dist/routes/tenant-credentials.js.map +1 -1
  86. package/dist/routes/work-items.js +654 -862
  87. package/dist/routes/work-items.js.map +1 -1
  88. package/dist/rules/binding-context.js +23 -35
  89. package/dist/rules/binding-context.js.map +1 -1
  90. package/dist/rules/defaults.js +200 -309
  91. package/dist/rules/defaults.js.map +1 -1
  92. package/dist/rules/dispatcher.d.ts.map +1 -1
  93. package/dist/rules/dispatcher.js +438 -677
  94. package/dist/rules/dispatcher.js.map +1 -1
  95. package/dist/rules/index.js +5 -599
  96. package/dist/rules/processor.js +344 -596
  97. package/dist/rules/processor.js.map +1 -1
  98. package/dist/rules/resolve.js +21 -18
  99. package/dist/rules/resolve.js.map +1 -1
  100. package/dist/rules/start-coordinator.js +168 -157
  101. package/dist/rules/start-coordinator.js.map +1 -1
  102. package/dist/rules/tools.js +50 -85
  103. package/dist/rules/tools.js.map +1 -1
  104. package/dist/rules/transition-service.js +183 -454
  105. package/dist/rules/transition-service.js.map +1 -1
  106. package/dist/rules/types.js +33 -29
  107. package/dist/rules/types.js.map +1 -1
  108. package/dist/rules/validation.js +266 -281
  109. package/dist/rules/validation.js.map +1 -1
  110. package/dist/sandbox/fleet.js +272 -250
  111. package/dist/sandbox/fleet.js.map +1 -1
  112. package/dist/sandbox/reattach.js +13 -6
  113. package/dist/sandbox/reattach.js.map +1 -1
  114. package/dist/server-error.js +12 -16
  115. package/dist/server-error.js.map +1 -1
  116. package/dist/skills/service.js +30 -35
  117. package/dist/skills/service.js.map +1 -1
  118. package/dist/spa-static.d.ts.map +1 -1
  119. package/dist/spa-static.js +91 -64
  120. package/dist/spa-static.js.map +1 -1
  121. package/dist/state-signing.js +73 -44
  122. package/dist/state-signing.js.map +1 -1
  123. package/dist/storage/domains/audit/agent-audit.js +60 -59
  124. package/dist/storage/domains/audit/agent-audit.js.map +1 -1
  125. package/dist/storage/domains/audit/base.js +169 -112
  126. package/dist/storage/domains/audit/base.js.map +1 -1
  127. package/dist/storage/domains/audit/domain.js +155 -153
  128. package/dist/storage/domains/audit/domain.js.map +1 -1
  129. package/dist/storage/domains/credentials/base.js +259 -243
  130. package/dist/storage/domains/credentials/base.js.map +1 -1
  131. package/dist/storage/domains/custom-providers/base.js +138 -143
  132. package/dist/storage/domains/custom-providers/base.js.map +1 -1
  133. package/dist/storage/domains/intake/base.js +75 -57
  134. package/dist/storage/domains/intake/base.js.map +1 -1
  135. package/dist/storage/domains/integrations/base.js +310 -245
  136. package/dist/storage/domains/integrations/base.js.map +1 -1
  137. package/dist/storage/domains/memory-settings/base.js +117 -102
  138. package/dist/storage/domains/memory-settings/base.js.map +1 -1
  139. package/dist/storage/domains/model-packs/base.js +89 -88
  140. package/dist/storage/domains/model-packs/base.js.map +1 -1
  141. package/dist/storage/domains/projects/base.js +97 -92
  142. package/dist/storage/domains/projects/base.js.map +1 -1
  143. package/dist/storage/domains/queue-health/base.js +111 -76
  144. package/dist/storage/domains/queue-health/base.js.map +1 -1
  145. package/dist/storage/domains/source-control/base.d.ts +9 -0
  146. package/dist/storage/domains/source-control/base.d.ts.map +1 -1
  147. package/dist/storage/domains/source-control/base.js +777 -702
  148. package/dist/storage/domains/source-control/base.js.map +1 -1
  149. package/dist/storage/domains/source-control/inmemory.d.ts +4 -0
  150. package/dist/storage/domains/source-control/inmemory.d.ts.map +1 -1
  151. package/dist/storage/domains/source-control/inmemory.js +322 -344
  152. package/dist/storage/domains/source-control/inmemory.js.map +1 -1
  153. package/dist/storage/domains/work-items/base.d.ts.map +1 -1
  154. package/dist/storage/domains/work-items/base.js +1266 -1132
  155. package/dist/storage/domains/work-items/base.js.map +1 -1
  156. package/dist/storage/domains/work-items/metrics.js +202 -184
  157. package/dist/storage/domains/work-items/metrics.js.map +1 -1
  158. package/dist/timing.d.ts +15 -0
  159. package/dist/timing.d.ts.map +1 -0
  160. package/dist/timing.js +34 -0
  161. package/dist/timing.js.map +1 -0
  162. package/dist/workspace.d.ts +2 -3
  163. package/dist/workspace.d.ts.map +1 -1
  164. package/dist/workspace.js +236 -438
  165. package/dist/workspace.js.map +1 -1
  166. package/factory-skills/factory-review/SKILL.md +9 -2
  167. package/package.json +9 -9
  168. package/dist/capabilities/connection.js.map +0 -1
  169. package/dist/capabilities/intake.js.map +0 -1
  170. package/dist/capabilities/version-control.js.map +0 -1
  171. package/dist/index.js.map +0 -1
  172. package/dist/integrations/base.js.map +0 -1
  173. package/dist/integrations/linear/storage.js.map +0 -1
  174. package/dist/rules/index.js.map +0 -1
@@ -1,310 +1,295 @@
1
- // src/rules/types.ts
2
- var FACTORY_RULE_STAGES = ["intake", "triage", "planning", "execute", "review", "done", "canceled"];
3
- var FACTORY_RULE_BOARDS = ["work", "review"];
4
- var FACTORY_RULE_SOURCES = ["issue", "pullRequest", "linearIssue", "manual"];
5
- var FACTORY_GITHUB_EVENTS = [
6
- "issueOpened",
7
- "pullRequestOpened",
8
- "pullRequestUpdated",
9
- "pullRequestReviewRequested",
10
- "pullRequestMerged"
1
+ import { FACTORY_GITHUB_EVENTS, FACTORY_LINEAR_EVENTS, FACTORY_RULE_BOARDS, FACTORY_RULE_SOURCES, FACTORY_RULE_STAGES } from "./types.js";
2
+ //#region src/rules/validation.ts
3
+ const MAX_FACTORY_RULE_CAUSAL_DEPTH = 8;
4
+ const MAX_VERSION_LENGTH = 128;
5
+ const MAX_IDEMPOTENCY_KEY_LENGTH = 256;
6
+ const MAX_REASON_LENGTH = 512;
7
+ const MAX_TITLE_LENGTH = 512;
8
+ const MAX_MESSAGE_LENGTH = 8192;
9
+ const MAX_ARGUMENTS_LENGTH = 4096;
10
+ const MAX_ROLE_LENGTH = 32;
11
+ const MAX_SKILL_NAME_LENGTH = 128;
12
+ const MAX_SOURCE_KEY_LENGTH = 256;
13
+ const MAX_URL_LENGTH = 2048;
14
+ const MAX_METADATA_JSON_LENGTH = 16384;
15
+ const MAX_JSON_DEPTH = 8;
16
+ const MAX_JSON_COLLECTION_SIZE = 100;
17
+ const IDENTIFIER_RE = /^[a-z0-9][a-z0-9_-]*$/i;
18
+ const SKILL_NAME_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
19
+ const SENSITIVE_KEY_RE = /(?:authorization|cookie|credential|password|secret|token)/i;
20
+ const WORK_ITEM_SOURCES = [
21
+ "github-issue",
22
+ "github-pr",
23
+ "linear-issue",
24
+ "manual"
11
25
  ];
12
- var FACTORY_LINEAR_EVENTS = ["issueObserved"];
13
-
14
- // src/rules/validation.ts
15
- var MAX_FACTORY_RULE_CAUSAL_DEPTH = 8;
16
- var MAX_VERSION_LENGTH = 128;
17
- var MAX_IDEMPOTENCY_KEY_LENGTH = 256;
18
- var MAX_REASON_LENGTH = 512;
19
- var MAX_TITLE_LENGTH = 512;
20
- var MAX_MESSAGE_LENGTH = 8192;
21
- var MAX_ARGUMENTS_LENGTH = 4096;
22
- var MAX_ROLE_LENGTH = 32;
23
- var MAX_SKILL_NAME_LENGTH = 128;
24
- var MAX_SOURCE_KEY_LENGTH = 256;
25
- var MAX_URL_LENGTH = 2048;
26
- var MAX_METADATA_JSON_LENGTH = 16384;
27
- var MAX_JSON_DEPTH = 8;
28
- var MAX_JSON_COLLECTION_SIZE = 100;
29
- var IDENTIFIER_RE = /^[a-z0-9][a-z0-9_-]*$/i;
30
- var SKILL_NAME_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
31
- var SENSITIVE_KEY_RE = /(?:authorization|cookie|credential|password|secret|token)/i;
32
- var WORK_ITEM_SOURCES = ["github-issue", "github-pr", "linear-issue", "manual"];
33
- var REJECTION_CODES = [
34
- "forbidden",
35
- "invalid_transition",
36
- "missing_binding",
37
- "stale",
38
- "timeout",
39
- "rule_error",
40
- "causal_depth_exceeded",
41
- "repeated_transition"
26
+ const REJECTION_CODES = [
27
+ "forbidden",
28
+ "invalid_transition",
29
+ "missing_binding",
30
+ "stale",
31
+ "timeout",
32
+ "rule_error",
33
+ "causal_depth_exceeded",
34
+ "repeated_transition"
42
35
  ];
43
36
  var FactoryRuleValidationError = class extends Error {
44
- code = "invalid_factory_rule";
45
- constructor(message) {
46
- super(message);
47
- this.name = "FactoryRuleValidationError";
48
- }
37
+ code = "invalid_factory_rule";
38
+ constructor(message) {
39
+ super(message);
40
+ this.name = "FactoryRuleValidationError";
41
+ }
49
42
  };
50
43
  function isPlainObject(value) {
51
- if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
52
- const prototype = Object.getPrototypeOf(value);
53
- return prototype === Object.prototype || prototype === null;
44
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
45
+ const prototype = Object.getPrototypeOf(value);
46
+ return prototype === Object.prototype || prototype === null;
54
47
  }
55
48
  function assertExactKeys(value, keys, label) {
56
- const allowed = new Set(keys);
57
- if (Object.keys(value).some((key) => !allowed.has(key))) {
58
- throw new FactoryRuleValidationError(`${label} contains an unsupported field.`);
59
- }
49
+ const allowed = new Set(keys);
50
+ if (Object.keys(value).some((key) => !allowed.has(key))) throw new FactoryRuleValidationError(`${label} contains an unsupported field.`);
60
51
  }
61
52
  function boundedString(value, label, max, pattern) {
62
- if (typeof value !== "string") throw new FactoryRuleValidationError(`${label} must be a string.`);
63
- const normalized = value.trim();
64
- if (normalized.length === 0 || normalized.length > max || pattern && !pattern.test(normalized)) {
65
- throw new FactoryRuleValidationError(`${label} is invalid.`);
66
- }
67
- return normalized;
53
+ if (typeof value !== "string") throw new FactoryRuleValidationError(`${label} must be a string.`);
54
+ const normalized = value.trim();
55
+ if (normalized.length === 0 || normalized.length > max || pattern && !pattern.test(normalized)) throw new FactoryRuleValidationError(`${label} is invalid.`);
56
+ return normalized;
68
57
  }
69
58
  function optionalBoundedString(value, label, max) {
70
- if (value === void 0) return void 0;
71
- return boundedString(value, label, max);
59
+ if (value === void 0) return void 0;
60
+ return boundedString(value, label, max);
72
61
  }
73
62
  function enumValue(value, allowed, label) {
74
- if (typeof value !== "string" || !allowed.includes(value)) {
75
- throw new FactoryRuleValidationError(`${label} is invalid.`);
76
- }
77
- return value;
63
+ if (typeof value !== "string" || !allowed.includes(value)) throw new FactoryRuleValidationError(`${label} is invalid.`);
64
+ return value;
78
65
  }
79
66
  function normalizeFactoryRuleJsonValue(value, depth = 0, seen = /* @__PURE__ */ new Set()) {
80
- if (value === null || typeof value === "boolean" || typeof value === "string") return value;
81
- if (typeof value === "number") {
82
- if (!Number.isFinite(value)) throw new FactoryRuleValidationError("Rule metadata must contain finite numbers.");
83
- return value;
84
- }
85
- if (depth >= MAX_JSON_DEPTH || typeof value !== "object" && !Array.isArray(value)) {
86
- throw new FactoryRuleValidationError("Rule metadata is not bounded JSON.");
87
- }
88
- if (seen.has(value)) throw new FactoryRuleValidationError("Rule metadata must not contain cycles.");
89
- seen.add(value);
90
- try {
91
- if (Array.isArray(value)) {
92
- if (value.length > MAX_JSON_COLLECTION_SIZE) {
93
- throw new FactoryRuleValidationError("Rule metadata contains too many entries.");
94
- }
95
- return value.map((entry) => normalizeFactoryRuleJsonValue(entry, depth + 1, seen));
96
- }
97
- if (!isPlainObject(value)) throw new FactoryRuleValidationError("Rule metadata must use plain objects.");
98
- const entries = Object.entries(value);
99
- if (entries.length > MAX_JSON_COLLECTION_SIZE) {
100
- throw new FactoryRuleValidationError("Rule metadata contains too many fields.");
101
- }
102
- const sanitized = {};
103
- for (const [key, entry] of entries) {
104
- const normalizedKey = boundedString(key, "Rule metadata key", 128, IDENTIFIER_RE);
105
- sanitized[normalizedKey] = SENSITIVE_KEY_RE.test(normalizedKey) ? "[REDACTED]" : normalizeFactoryRuleJsonValue(entry, depth + 1, seen);
106
- }
107
- return sanitized;
108
- } finally {
109
- seen.delete(value);
110
- }
67
+ if (value === null || typeof value === "boolean" || typeof value === "string") return value;
68
+ if (typeof value === "number") {
69
+ if (!Number.isFinite(value)) throw new FactoryRuleValidationError("Rule metadata must contain finite numbers.");
70
+ return value;
71
+ }
72
+ if (depth >= MAX_JSON_DEPTH || typeof value !== "object" && !Array.isArray(value)) throw new FactoryRuleValidationError("Rule metadata is not bounded JSON.");
73
+ if (seen.has(value)) throw new FactoryRuleValidationError("Rule metadata must not contain cycles.");
74
+ seen.add(value);
75
+ try {
76
+ if (Array.isArray(value)) {
77
+ if (value.length > MAX_JSON_COLLECTION_SIZE) throw new FactoryRuleValidationError("Rule metadata contains too many entries.");
78
+ return value.map((entry) => normalizeFactoryRuleJsonValue(entry, depth + 1, seen));
79
+ }
80
+ if (!isPlainObject(value)) throw new FactoryRuleValidationError("Rule metadata must use plain objects.");
81
+ const entries = Object.entries(value);
82
+ if (entries.length > MAX_JSON_COLLECTION_SIZE) throw new FactoryRuleValidationError("Rule metadata contains too many fields.");
83
+ const sanitized = {};
84
+ for (const [key, entry] of entries) {
85
+ const normalizedKey = boundedString(key, "Rule metadata key", 128, IDENTIFIER_RE);
86
+ sanitized[normalizedKey] = SENSITIVE_KEY_RE.test(normalizedKey) ? "[REDACTED]" : normalizeFactoryRuleJsonValue(entry, depth + 1, seen);
87
+ }
88
+ return sanitized;
89
+ } finally {
90
+ seen.delete(value);
91
+ }
111
92
  }
112
93
  function sanitizeMetadata(value) {
113
- if (value === void 0) return void 0;
114
- const sanitized = normalizeFactoryRuleJsonValue(value);
115
- if (!isPlainObject(sanitized)) throw new FactoryRuleValidationError("Rule metadata must be an object.");
116
- if (JSON.stringify(sanitized).length > MAX_METADATA_JSON_LENGTH) {
117
- throw new FactoryRuleValidationError("Rule metadata is too large.");
118
- }
119
- return sanitized;
94
+ if (value === void 0) return void 0;
95
+ const sanitized = normalizeFactoryRuleJsonValue(value);
96
+ if (!isPlainObject(sanitized)) throw new FactoryRuleValidationError("Rule metadata must be an object.");
97
+ if (JSON.stringify(sanitized).length > MAX_METADATA_JSON_LENGTH) throw new FactoryRuleValidationError("Rule metadata is too large.");
98
+ return sanitized;
120
99
  }
121
100
  function validateBoardRules(rules, label) {
122
- if (!isPlainObject(rules)) throw new FactoryRuleValidationError(`${label} must be an object.`);
123
- for (const [stage, sources] of Object.entries(rules)) {
124
- enumValue(stage, FACTORY_RULE_STAGES, `${label} stage`);
125
- if (!isPlainObject(sources)) throw new FactoryRuleValidationError(`${label}.${stage} must be an object.`);
126
- for (const [source, leaf] of Object.entries(sources)) {
127
- enumValue(source, FACTORY_RULE_SOURCES, `${label}.${stage} source`);
128
- if (!isPlainObject(leaf)) throw new FactoryRuleValidationError(`${label}.${stage}.${source} must be an object.`);
129
- assertExactKeys(leaf, ["onEnter", "onExit"], `${label}.${stage}.${source}`);
130
- for (const handler of Object.values(leaf)) {
131
- if (handler !== void 0 && typeof handler !== "function") {
132
- throw new FactoryRuleValidationError(`${label}.${stage}.${source} handlers must be functions.`);
133
- }
134
- }
135
- }
136
- }
101
+ if (!isPlainObject(rules)) throw new FactoryRuleValidationError(`${label} must be an object.`);
102
+ for (const [stage, sources] of Object.entries(rules)) {
103
+ enumValue(stage, FACTORY_RULE_STAGES, `${label} stage`);
104
+ if (!isPlainObject(sources)) throw new FactoryRuleValidationError(`${label}.${stage} must be an object.`);
105
+ for (const [source, leaf] of Object.entries(sources)) {
106
+ enumValue(source, FACTORY_RULE_SOURCES, `${label}.${stage} source`);
107
+ if (!isPlainObject(leaf)) throw new FactoryRuleValidationError(`${label}.${stage}.${source} must be an object.`);
108
+ assertExactKeys(leaf, ["onEnter", "onExit"], `${label}.${stage}.${source}`);
109
+ for (const handler of Object.values(leaf)) if (handler !== void 0 && typeof handler !== "function") throw new FactoryRuleValidationError(`${label}.${stage}.${source} handlers must be functions.`);
110
+ }
111
+ }
137
112
  }
138
113
  function assertFactoryRules(rules) {
139
- if (!isPlainObject(rules)) throw new FactoryRuleValidationError("Factory rules must be an object.");
140
- assertExactKeys(rules, ["version", "work", "review", "tools", "github", "linear"], "Factory rules");
141
- boundedString(rules.version, "Factory rule version", MAX_VERSION_LENGTH);
142
- validateBoardRules(rules.work, "Factory rules.work");
143
- validateBoardRules(rules.review, "Factory rules.review");
144
- if (!isPlainObject(rules.tools)) throw new FactoryRuleValidationError("Factory rules.tools must be an object.");
145
- for (const [toolName, leaf] of Object.entries(rules.tools)) {
146
- boundedString(toolName, "Factory tool name", 128, IDENTIFIER_RE);
147
- if (!isPlainObject(leaf))
148
- throw new FactoryRuleValidationError(`Factory rules.tools.${toolName} must be an object.`);
149
- assertExactKeys(leaf, ["onResult"], `Factory rules.tools.${toolName}`);
150
- if (leaf.onResult !== void 0 && typeof leaf.onResult !== "function") {
151
- throw new FactoryRuleValidationError(`Factory rules.tools.${toolName}.onResult must be a function.`);
152
- }
153
- }
154
- if (!isPlainObject(rules.github)) throw new FactoryRuleValidationError("Factory rules.github must be an object.");
155
- for (const [event, leaf] of Object.entries(rules.github)) {
156
- enumValue(event, FACTORY_GITHUB_EVENTS, "Factory GitHub event");
157
- if (!isPlainObject(leaf)) throw new FactoryRuleValidationError(`Factory rules.github.${event} must be an object.`);
158
- assertExactKeys(leaf, ["onEvent"], `Factory rules.github.${event}`);
159
- if (leaf.onEvent !== void 0 && typeof leaf.onEvent !== "function") {
160
- throw new FactoryRuleValidationError(`Factory rules.github.${event}.onEvent must be a function.`);
161
- }
162
- }
163
- if (!isPlainObject(rules.linear)) throw new FactoryRuleValidationError("Factory rules.linear must be an object.");
164
- for (const [event, leaf] of Object.entries(rules.linear)) {
165
- enumValue(event, FACTORY_LINEAR_EVENTS, "Factory Linear event");
166
- if (!isPlainObject(leaf)) throw new FactoryRuleValidationError(`Factory rules.linear.${event} must be an object.`);
167
- assertExactKeys(leaf, ["onEvent"], `Factory rules.linear.${event}`);
168
- if (leaf.onEvent !== void 0 && typeof leaf.onEvent !== "function") {
169
- throw new FactoryRuleValidationError(`Factory rules.linear.${event}.onEvent must be a function.`);
170
- }
171
- }
114
+ if (!isPlainObject(rules)) throw new FactoryRuleValidationError("Factory rules must be an object.");
115
+ assertExactKeys(rules, [
116
+ "version",
117
+ "work",
118
+ "review",
119
+ "tools",
120
+ "github",
121
+ "linear"
122
+ ], "Factory rules");
123
+ boundedString(rules.version, "Factory rule version", MAX_VERSION_LENGTH);
124
+ validateBoardRules(rules.work, "Factory rules.work");
125
+ validateBoardRules(rules.review, "Factory rules.review");
126
+ if (!isPlainObject(rules.tools)) throw new FactoryRuleValidationError("Factory rules.tools must be an object.");
127
+ for (const [toolName, leaf] of Object.entries(rules.tools)) {
128
+ boundedString(toolName, "Factory tool name", 128, IDENTIFIER_RE);
129
+ if (!isPlainObject(leaf)) throw new FactoryRuleValidationError(`Factory rules.tools.${toolName} must be an object.`);
130
+ assertExactKeys(leaf, ["onResult"], `Factory rules.tools.${toolName}`);
131
+ if (leaf.onResult !== void 0 && typeof leaf.onResult !== "function") throw new FactoryRuleValidationError(`Factory rules.tools.${toolName}.onResult must be a function.`);
132
+ }
133
+ if (!isPlainObject(rules.github)) throw new FactoryRuleValidationError("Factory rules.github must be an object.");
134
+ for (const [event, leaf] of Object.entries(rules.github)) {
135
+ enumValue(event, FACTORY_GITHUB_EVENTS, "Factory GitHub event");
136
+ if (!isPlainObject(leaf)) throw new FactoryRuleValidationError(`Factory rules.github.${event} must be an object.`);
137
+ assertExactKeys(leaf, ["onEvent"], `Factory rules.github.${event}`);
138
+ if (leaf.onEvent !== void 0 && typeof leaf.onEvent !== "function") throw new FactoryRuleValidationError(`Factory rules.github.${event}.onEvent must be a function.`);
139
+ }
140
+ if (!isPlainObject(rules.linear)) throw new FactoryRuleValidationError("Factory rules.linear must be an object.");
141
+ for (const [event, leaf] of Object.entries(rules.linear)) {
142
+ enumValue(event, FACTORY_LINEAR_EVENTS, "Factory Linear event");
143
+ if (!isPlainObject(leaf)) throw new FactoryRuleValidationError(`Factory rules.linear.${event} must be an object.`);
144
+ assertExactKeys(leaf, ["onEvent"], `Factory rules.linear.${event}`);
145
+ if (leaf.onEvent !== void 0 && typeof leaf.onEvent !== "function") throw new FactoryRuleValidationError(`Factory rules.linear.${event}.onEvent must be a function.`);
146
+ }
172
147
  }
173
148
  function commonCommitFields(value) {
174
- return {
175
- idempotencyKey: boundedString(value.idempotencyKey, "Factory decision idempotencyKey", MAX_IDEMPOTENCY_KEY_LENGTH)
176
- };
149
+ return { idempotencyKey: boundedString(value.idempotencyKey, "Factory decision idempotencyKey", MAX_IDEMPOTENCY_KEY_LENGTH) };
177
150
  }
178
151
  function validateFactoryRuleDecision(value, causalDepth = 0) {
179
- if (causalDepth > MAX_FACTORY_RULE_CAUSAL_DEPTH) {
180
- throw new FactoryRuleValidationError("Factory rule causal depth exceeded.");
181
- }
182
- if (!isPlainObject(value)) throw new FactoryRuleValidationError("Factory rule decision must be an object.");
183
- const type = value.type;
184
- if (typeof type !== "string") throw new FactoryRuleValidationError("Factory rule decision type is required.");
185
- switch (type) {
186
- case "reject": {
187
- assertExactKeys(value, ["type", "code", "reason"], "Factory reject decision");
188
- return {
189
- type,
190
- code: enumValue(value.code, REJECTION_CODES, "Factory rejection code"),
191
- reason: boundedString(value.reason, "Factory rejection reason", MAX_REASON_LENGTH)
192
- };
193
- }
194
- case "transition": {
195
- assertExactKeys(value, ["type", "idempotencyKey", "board", "stage"], "Factory transition decision");
196
- return {
197
- type,
198
- ...commonCommitFields(value),
199
- board: enumValue(value.board, FACTORY_RULE_BOARDS, "Factory transition board"),
200
- stage: enumValue(value.stage, FACTORY_RULE_STAGES, "Factory transition stage")
201
- };
202
- }
203
- case "upsertLinkedWorkItem": {
204
- assertExactKeys(
205
- value,
206
- ["type", "idempotencyKey", "board", "source", "sourceKey", "title", "url", "stage", "metadata"],
207
- "Factory linked work item decision"
208
- );
209
- const url = value.url;
210
- if (url !== null && (typeof url !== "string" || url.length > MAX_URL_LENGTH || !/^https?:\/\//.test(url))) {
211
- throw new FactoryRuleValidationError("Factory linked work item URL is invalid.");
212
- }
213
- const metadata = sanitizeMetadata(value.metadata);
214
- return {
215
- type,
216
- ...commonCommitFields(value),
217
- board: enumValue(value.board, FACTORY_RULE_BOARDS, "Factory linked work item board"),
218
- source: enumValue(value.source, WORK_ITEM_SOURCES, "Factory linked work item source"),
219
- sourceKey: boundedString(value.sourceKey, "Factory linked work item sourceKey", MAX_SOURCE_KEY_LENGTH),
220
- title: boundedString(value.title, "Factory linked work item title", MAX_TITLE_LENGTH),
221
- url,
222
- stage: enumValue(value.stage, FACTORY_RULE_STAGES, "Factory linked work item stage"),
223
- ...metadata ? { metadata } : {}
224
- };
225
- }
226
- case "invokeSkill": {
227
- assertExactKeys(
228
- value,
229
- ["type", "idempotencyKey", "role", "skillName", "arguments", "precedingMessage"],
230
- "Factory invoke skill decision"
231
- );
232
- const args = optionalBoundedString(value.arguments, "Factory skill arguments", MAX_ARGUMENTS_LENGTH);
233
- const precedingMessage = optionalBoundedString(
234
- value.precedingMessage,
235
- "Factory skill preceding message",
236
- MAX_MESSAGE_LENGTH
237
- );
238
- return {
239
- type,
240
- ...commonCommitFields(value),
241
- role: boundedString(value.role, "Factory skill role", MAX_ROLE_LENGTH, IDENTIFIER_RE),
242
- skillName: boundedString(value.skillName, "Factory skill name", MAX_SKILL_NAME_LENGTH, SKILL_NAME_RE),
243
- ...args ? { arguments: args } : {},
244
- ...precedingMessage ? { precedingMessage } : {}
245
- };
246
- }
247
- case "sendMessage": {
248
- assertExactKeys(
249
- value,
250
- ["type", "idempotencyKey", "role", "message", "priority", "idleBehavior", "prepareBinding"],
251
- "Factory send message decision"
252
- );
253
- const priority = value.priority === void 0 ? void 0 : enumValue(value.priority, ["medium", "high", "urgent"], "Factory message priority");
254
- const idleBehavior = value.idleBehavior === void 0 ? void 0 : enumValue(value.idleBehavior, ["persist", "wake"], "Factory message idle behavior");
255
- if (value.prepareBinding !== void 0 && typeof value.prepareBinding !== "boolean") {
256
- throw new FactoryRuleValidationError("Factory message prepareBinding must be a boolean.");
257
- }
258
- return {
259
- type,
260
- ...commonCommitFields(value),
261
- role: boundedString(value.role, "Factory message role", MAX_ROLE_LENGTH, IDENTIFIER_RE),
262
- message: boundedString(value.message, "Factory message", MAX_MESSAGE_LENGTH),
263
- ...priority ? { priority } : {},
264
- ...idleBehavior ? { idleBehavior } : {},
265
- ...value.prepareBinding === true ? { prepareBinding: true } : {}
266
- };
267
- }
268
- case "notify": {
269
- assertExactKeys(value, ["type", "idempotencyKey", "title", "body", "level"], "Factory notify decision");
270
- const body = optionalBoundedString(value.body, "Factory notification body", MAX_MESSAGE_LENGTH);
271
- const level = value.level === void 0 ? void 0 : enumValue(value.level, ["info", "warning", "error"], "Factory notification level");
272
- return {
273
- type,
274
- ...commonCommitFields(value),
275
- title: boundedString(value.title, "Factory notification title", MAX_TITLE_LENGTH),
276
- ...body ? { body } : {},
277
- ...level ? { level } : {}
278
- };
279
- }
280
- default:
281
- throw new FactoryRuleValidationError("Factory rule decision type is unsupported.");
282
- }
152
+ if (causalDepth > 8) throw new FactoryRuleValidationError("Factory rule causal depth exceeded.");
153
+ if (!isPlainObject(value)) throw new FactoryRuleValidationError("Factory rule decision must be an object.");
154
+ const type = value.type;
155
+ if (typeof type !== "string") throw new FactoryRuleValidationError("Factory rule decision type is required.");
156
+ switch (type) {
157
+ case "reject":
158
+ assertExactKeys(value, [
159
+ "type",
160
+ "code",
161
+ "reason"
162
+ ], "Factory reject decision");
163
+ return {
164
+ type,
165
+ code: enumValue(value.code, REJECTION_CODES, "Factory rejection code"),
166
+ reason: boundedString(value.reason, "Factory rejection reason", MAX_REASON_LENGTH)
167
+ };
168
+ case "transition":
169
+ assertExactKeys(value, [
170
+ "type",
171
+ "idempotencyKey",
172
+ "board",
173
+ "stage"
174
+ ], "Factory transition decision");
175
+ return {
176
+ type,
177
+ ...commonCommitFields(value),
178
+ board: enumValue(value.board, FACTORY_RULE_BOARDS, "Factory transition board"),
179
+ stage: enumValue(value.stage, FACTORY_RULE_STAGES, "Factory transition stage")
180
+ };
181
+ case "upsertLinkedWorkItem": {
182
+ assertExactKeys(value, [
183
+ "type",
184
+ "idempotencyKey",
185
+ "board",
186
+ "source",
187
+ "sourceKey",
188
+ "title",
189
+ "url",
190
+ "stage",
191
+ "metadata"
192
+ ], "Factory linked work item decision");
193
+ const url = value.url;
194
+ if (url !== null && (typeof url !== "string" || url.length > MAX_URL_LENGTH || !/^https?:\/\//.test(url))) throw new FactoryRuleValidationError("Factory linked work item URL is invalid.");
195
+ const metadata = sanitizeMetadata(value.metadata);
196
+ return {
197
+ type,
198
+ ...commonCommitFields(value),
199
+ board: enumValue(value.board, FACTORY_RULE_BOARDS, "Factory linked work item board"),
200
+ source: enumValue(value.source, WORK_ITEM_SOURCES, "Factory linked work item source"),
201
+ sourceKey: boundedString(value.sourceKey, "Factory linked work item sourceKey", MAX_SOURCE_KEY_LENGTH),
202
+ title: boundedString(value.title, "Factory linked work item title", MAX_TITLE_LENGTH),
203
+ url,
204
+ stage: enumValue(value.stage, FACTORY_RULE_STAGES, "Factory linked work item stage"),
205
+ ...metadata ? { metadata } : {}
206
+ };
207
+ }
208
+ case "invokeSkill": {
209
+ assertExactKeys(value, [
210
+ "type",
211
+ "idempotencyKey",
212
+ "role",
213
+ "skillName",
214
+ "arguments",
215
+ "precedingMessage"
216
+ ], "Factory invoke skill decision");
217
+ const args = optionalBoundedString(value.arguments, "Factory skill arguments", MAX_ARGUMENTS_LENGTH);
218
+ const precedingMessage = optionalBoundedString(value.precedingMessage, "Factory skill preceding message", MAX_MESSAGE_LENGTH);
219
+ return {
220
+ type,
221
+ ...commonCommitFields(value),
222
+ role: boundedString(value.role, "Factory skill role", MAX_ROLE_LENGTH, IDENTIFIER_RE),
223
+ skillName: boundedString(value.skillName, "Factory skill name", MAX_SKILL_NAME_LENGTH, SKILL_NAME_RE),
224
+ ...args ? { arguments: args } : {},
225
+ ...precedingMessage ? { precedingMessage } : {}
226
+ };
227
+ }
228
+ case "sendMessage": {
229
+ assertExactKeys(value, [
230
+ "type",
231
+ "idempotencyKey",
232
+ "role",
233
+ "message",
234
+ "priority",
235
+ "idleBehavior",
236
+ "prepareBinding"
237
+ ], "Factory send message decision");
238
+ const priority = value.priority === void 0 ? void 0 : enumValue(value.priority, [
239
+ "medium",
240
+ "high",
241
+ "urgent"
242
+ ], "Factory message priority");
243
+ const idleBehavior = value.idleBehavior === void 0 ? void 0 : enumValue(value.idleBehavior, ["persist", "wake"], "Factory message idle behavior");
244
+ if (value.prepareBinding !== void 0 && typeof value.prepareBinding !== "boolean") throw new FactoryRuleValidationError("Factory message prepareBinding must be a boolean.");
245
+ return {
246
+ type,
247
+ ...commonCommitFields(value),
248
+ role: boundedString(value.role, "Factory message role", MAX_ROLE_LENGTH, IDENTIFIER_RE),
249
+ message: boundedString(value.message, "Factory message", MAX_MESSAGE_LENGTH),
250
+ ...priority ? { priority } : {},
251
+ ...idleBehavior ? { idleBehavior } : {},
252
+ ...value.prepareBinding === true ? { prepareBinding: true } : {}
253
+ };
254
+ }
255
+ case "notify": {
256
+ assertExactKeys(value, [
257
+ "type",
258
+ "idempotencyKey",
259
+ "title",
260
+ "body",
261
+ "level"
262
+ ], "Factory notify decision");
263
+ const body = optionalBoundedString(value.body, "Factory notification body", MAX_MESSAGE_LENGTH);
264
+ const level = value.level === void 0 ? void 0 : enumValue(value.level, [
265
+ "info",
266
+ "warning",
267
+ "error"
268
+ ], "Factory notification level");
269
+ return {
270
+ type,
271
+ ...commonCommitFields(value),
272
+ title: boundedString(value.title, "Factory notification title", MAX_TITLE_LENGTH),
273
+ ...body ? { body } : {},
274
+ ...level ? { level } : {}
275
+ };
276
+ }
277
+ default: throw new FactoryRuleValidationError("Factory rule decision type is unsupported.");
278
+ }
283
279
  }
284
280
  function validateFactoryRuleDecisions(values, causalDepth = 0) {
285
- if (values.length > MAX_JSON_COLLECTION_SIZE) {
286
- throw new FactoryRuleValidationError("Factory rule produced too many decisions.");
287
- }
288
- const decisions = [];
289
- for (const value of values) {
290
- const decision = validateFactoryRuleDecision(value, causalDepth);
291
- if (decision.type === "reject") {
292
- throw new FactoryRuleValidationError("A rejection cannot be persisted with commit decisions.");
293
- }
294
- decisions.push(decision);
295
- }
296
- const keys = decisions.map((decision) => decision.idempotencyKey);
297
- if (new Set(keys).size !== keys.length) {
298
- throw new FactoryRuleValidationError("Factory decisions require unique idempotency keys.");
299
- }
300
- return decisions;
281
+ if (values.length > MAX_JSON_COLLECTION_SIZE) throw new FactoryRuleValidationError("Factory rule produced too many decisions.");
282
+ const decisions = [];
283
+ for (const value of values) {
284
+ const decision = validateFactoryRuleDecision(value, causalDepth);
285
+ if (decision.type === "reject") throw new FactoryRuleValidationError("A rejection cannot be persisted with commit decisions.");
286
+ decisions.push(decision);
287
+ }
288
+ const keys = decisions.map((decision) => decision.idempotencyKey);
289
+ if (new Set(keys).size !== keys.length) throw new FactoryRuleValidationError("Factory decisions require unique idempotency keys.");
290
+ return decisions;
301
291
  }
302
- export {
303
- FactoryRuleValidationError,
304
- MAX_FACTORY_RULE_CAUSAL_DEPTH,
305
- assertFactoryRules,
306
- normalizeFactoryRuleJsonValue,
307
- validateFactoryRuleDecision,
308
- validateFactoryRuleDecisions
309
- };
292
+ //#endregion
293
+ export { FactoryRuleValidationError, MAX_FACTORY_RULE_CAUSAL_DEPTH, assertFactoryRules, normalizeFactoryRuleJsonValue, validateFactoryRuleDecision, validateFactoryRuleDecisions };
294
+
310
295
  //# sourceMappingURL=validation.js.map