@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.
- package/CHANGELOG.md +36 -0
- package/dist/auth.d.ts +2 -2
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +401 -314
- package/dist/auth.js.map +1 -1
- package/dist/capabilities/connection.js +1 -1
- package/dist/capabilities/intake.js +1 -1
- package/dist/capabilities/version-control.js +1 -1
- package/dist/factory.d.ts.map +1 -1
- package/dist/factory.js +441 -39455
- package/dist/factory.js.map +1 -1
- package/dist/index.js +2 -39466
- package/dist/integrations/base.js +1 -1
- package/dist/integrations/github/config.js +36 -22
- package/dist/integrations/github/config.js.map +1 -1
- package/dist/integrations/github/factory-session.js +33 -37
- package/dist/integrations/github/factory-session.js.map +1 -1
- package/dist/integrations/github/integration.js +1037 -3624
- package/dist/integrations/github/integration.js.map +1 -1
- package/dist/integrations/github/issue-triage.js +71 -71
- package/dist/integrations/github/issue-triage.js.map +1 -1
- package/dist/integrations/github/pat.js +47 -36
- package/dist/integrations/github/pat.js.map +1 -1
- package/dist/integrations/github/provenance.js +72 -101
- package/dist/integrations/github/provenance.js.map +1 -1
- package/dist/integrations/github/routes.d.ts.map +1 -1
- package/dist/integrations/github/routes.js +1272 -1833
- package/dist/integrations/github/routes.js.map +1 -1
- package/dist/integrations/github/rules.js +203 -471
- package/dist/integrations/github/rules.js.map +1 -1
- package/dist/integrations/github/sandbox.d.ts +2 -0
- package/dist/integrations/github/sandbox.d.ts.map +1 -1
- package/dist/integrations/github/sandbox.js +401 -326
- package/dist/integrations/github/sandbox.js.map +1 -1
- package/dist/integrations/github/session-subscriptions.js +165 -223
- package/dist/integrations/github/session-subscriptions.js.map +1 -1
- package/dist/integrations/github/subscriptions.js +43 -51
- package/dist/integrations/github/subscriptions.js.map +1 -1
- package/dist/integrations/github/token-refresh.js +15 -18
- package/dist/integrations/github/token-refresh.js.map +1 -1
- package/dist/integrations/github/webhook.js +359 -343
- package/dist/integrations/github/webhook.js.map +1 -1
- package/dist/integrations/linear/agent-tools.js +77 -100
- package/dist/integrations/linear/agent-tools.js.map +1 -1
- package/dist/integrations/linear/integration.js +611 -1309
- package/dist/integrations/linear/integration.js.map +1 -1
- package/dist/integrations/linear/routes.js +227 -230
- package/dist/integrations/linear/routes.js.map +1 -1
- package/dist/integrations/linear/rules.js +126 -346
- package/dist/integrations/linear/rules.js.map +1 -1
- package/dist/integrations/linear/storage.js +1 -1
- package/dist/integrations/platform/api-client.js +131 -135
- package/dist/integrations/platform/api-client.js.map +1 -1
- package/dist/integrations/platform/github/event-worker.js +275 -573
- package/dist/integrations/platform/github/event-worker.js.map +1 -1
- package/dist/integrations/platform/github/integration.d.ts.map +1 -1
- package/dist/integrations/platform/github/integration.js +845 -3942
- package/dist/integrations/platform/github/integration.js.map +1 -1
- package/dist/integrations/platform/linear/integration.js +457 -1309
- package/dist/integrations/platform/linear/integration.js.map +1 -1
- package/dist/integrations/workos/integration.js +80 -85
- package/dist/integrations/workos/integration.js.map +1 -1
- package/dist/routes/config.d.ts.map +1 -1
- package/dist/routes/config.js +933 -915
- package/dist/routes/config.js.map +1 -1
- package/dist/routes/custom-provider-source.js +110 -93
- package/dist/routes/custom-provider-source.js.map +1 -1
- package/dist/routes/fs.js +401 -349
- package/dist/routes/fs.js.map +1 -1
- package/dist/routes/intake.js +165 -165
- package/dist/routes/intake.js.map +1 -1
- package/dist/routes/oauth.js +447 -25166
- package/dist/routes/oauth.js.map +1 -1
- package/dist/routes/projects.js +437 -376
- package/dist/routes/projects.js.map +1 -1
- package/dist/routes/provider-credentials.js +76 -58
- package/dist/routes/provider-credentials.js.map +1 -1
- package/dist/routes/route.js +12 -8
- package/dist/routes/route.js.map +1 -1
- package/dist/routes/skills.js +160 -173
- package/dist/routes/skills.js.map +1 -1
- package/dist/routes/surface.js +250 -28618
- package/dist/routes/surface.js.map +1 -1
- package/dist/routes/tenant-credentials.js +141 -166
- package/dist/routes/tenant-credentials.js.map +1 -1
- package/dist/routes/work-items.js +654 -862
- package/dist/routes/work-items.js.map +1 -1
- package/dist/rules/binding-context.js +23 -35
- package/dist/rules/binding-context.js.map +1 -1
- package/dist/rules/defaults.js +200 -309
- package/dist/rules/defaults.js.map +1 -1
- package/dist/rules/dispatcher.d.ts.map +1 -1
- package/dist/rules/dispatcher.js +438 -677
- package/dist/rules/dispatcher.js.map +1 -1
- package/dist/rules/index.js +5 -599
- package/dist/rules/processor.js +344 -596
- package/dist/rules/processor.js.map +1 -1
- package/dist/rules/resolve.js +21 -18
- package/dist/rules/resolve.js.map +1 -1
- package/dist/rules/start-coordinator.js +168 -157
- package/dist/rules/start-coordinator.js.map +1 -1
- package/dist/rules/tools.js +50 -85
- package/dist/rules/tools.js.map +1 -1
- package/dist/rules/transition-service.js +183 -454
- package/dist/rules/transition-service.js.map +1 -1
- package/dist/rules/types.js +33 -29
- package/dist/rules/types.js.map +1 -1
- package/dist/rules/validation.js +266 -281
- package/dist/rules/validation.js.map +1 -1
- package/dist/sandbox/fleet.js +272 -250
- package/dist/sandbox/fleet.js.map +1 -1
- package/dist/sandbox/reattach.js +13 -6
- package/dist/sandbox/reattach.js.map +1 -1
- package/dist/server-error.js +12 -16
- package/dist/server-error.js.map +1 -1
- package/dist/skills/service.js +30 -35
- package/dist/skills/service.js.map +1 -1
- package/dist/spa-static.d.ts.map +1 -1
- package/dist/spa-static.js +91 -64
- package/dist/spa-static.js.map +1 -1
- package/dist/state-signing.js +73 -44
- package/dist/state-signing.js.map +1 -1
- package/dist/storage/domains/audit/agent-audit.js +60 -59
- package/dist/storage/domains/audit/agent-audit.js.map +1 -1
- package/dist/storage/domains/audit/base.js +169 -112
- package/dist/storage/domains/audit/base.js.map +1 -1
- package/dist/storage/domains/audit/domain.js +155 -153
- package/dist/storage/domains/audit/domain.js.map +1 -1
- package/dist/storage/domains/credentials/base.js +259 -243
- package/dist/storage/domains/credentials/base.js.map +1 -1
- package/dist/storage/domains/custom-providers/base.js +138 -143
- package/dist/storage/domains/custom-providers/base.js.map +1 -1
- package/dist/storage/domains/intake/base.js +75 -57
- package/dist/storage/domains/intake/base.js.map +1 -1
- package/dist/storage/domains/integrations/base.js +310 -245
- package/dist/storage/domains/integrations/base.js.map +1 -1
- package/dist/storage/domains/memory-settings/base.js +117 -102
- package/dist/storage/domains/memory-settings/base.js.map +1 -1
- package/dist/storage/domains/model-packs/base.js +89 -88
- package/dist/storage/domains/model-packs/base.js.map +1 -1
- package/dist/storage/domains/projects/base.js +97 -92
- package/dist/storage/domains/projects/base.js.map +1 -1
- package/dist/storage/domains/queue-health/base.js +111 -76
- package/dist/storage/domains/queue-health/base.js.map +1 -1
- package/dist/storage/domains/source-control/base.d.ts +9 -0
- package/dist/storage/domains/source-control/base.d.ts.map +1 -1
- package/dist/storage/domains/source-control/base.js +777 -702
- package/dist/storage/domains/source-control/base.js.map +1 -1
- package/dist/storage/domains/source-control/inmemory.d.ts +4 -0
- package/dist/storage/domains/source-control/inmemory.d.ts.map +1 -1
- package/dist/storage/domains/source-control/inmemory.js +322 -344
- package/dist/storage/domains/source-control/inmemory.js.map +1 -1
- package/dist/storage/domains/work-items/base.d.ts.map +1 -1
- package/dist/storage/domains/work-items/base.js +1266 -1132
- package/dist/storage/domains/work-items/base.js.map +1 -1
- package/dist/storage/domains/work-items/metrics.js +202 -184
- package/dist/storage/domains/work-items/metrics.js.map +1 -1
- package/dist/timing.d.ts +15 -0
- package/dist/timing.d.ts.map +1 -0
- package/dist/timing.js +34 -0
- package/dist/timing.js.map +1 -0
- package/dist/workspace.d.ts +2 -3
- package/dist/workspace.d.ts.map +1 -1
- package/dist/workspace.js +236 -438
- package/dist/workspace.js.map +1 -1
- package/factory-skills/factory-review/SKILL.md +9 -2
- package/package.json +9 -9
- package/dist/capabilities/connection.js.map +0 -1
- package/dist/capabilities/intake.js.map +0 -1
- package/dist/capabilities/version-control.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/integrations/base.js.map +0 -1
- package/dist/integrations/linear/storage.js.map +0 -1
- package/dist/rules/index.js.map +0 -1
package/dist/rules/validation.js
CHANGED
|
@@ -1,310 +1,295 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
57
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
71
|
-
|
|
59
|
+
if (value === void 0) return void 0;
|
|
60
|
+
return boundedString(value, label, max);
|
|
72
61
|
}
|
|
73
62
|
function enumValue(value, allowed, label) {
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
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
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
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
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
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
|
-
|
|
303
|
-
|
|
304
|
-
|
|
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
|