@plures/praxis 1.3.0 → 1.4.4
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/dist/browser/{chunk-N63K4KWS.js → chunk-4IRUGWR3.js} +1 -1
- package/dist/browser/chunk-6SJ44Q64.js +473 -0
- package/dist/browser/chunk-BQOYZBWA.js +282 -0
- package/dist/browser/chunk-IG5BJ2MT.js +91 -0
- package/dist/browser/{chunk-MJK3IYTJ.js → chunk-JZDJU2DO.js} +4 -84
- package/dist/browser/chunk-ZEW4LJAJ.js +353 -0
- package/dist/browser/{engine-YIEGSX7U.js → engine-3B5WJPGT.js} +2 -1
- package/dist/browser/expectations/index.d.ts +180 -0
- package/dist/browser/expectations/index.js +14 -0
- package/dist/browser/factory/index.d.ts +149 -0
- package/dist/browser/factory/index.js +15 -0
- package/dist/browser/index.d.ts +274 -3
- package/dist/browser/index.js +407 -54
- package/dist/browser/integrations/svelte.d.ts +3 -2
- package/dist/browser/integrations/svelte.js +3 -2
- package/dist/browser/project/index.d.ts +176 -0
- package/dist/browser/project/index.js +19 -0
- package/dist/browser/reactive-engine.svelte-DgVTqHLc.d.ts +223 -0
- package/dist/browser/{reactive-engine.svelte-DjynI82A.d.ts → rules-i1LHpnGd.d.ts} +13 -221
- package/dist/node/chunk-2IUFZBH3.js +87 -0
- package/dist/node/{chunk-WZ6B3LZ6.js → chunk-7CSWBDFL.js} +3 -56
- package/dist/node/chunk-AZLNISFI.js +1690 -0
- package/dist/node/chunk-IG5BJ2MT.js +91 -0
- package/dist/node/{chunk-KMJWAFZV.js → chunk-JZDJU2DO.js} +4 -89
- package/dist/node/chunk-PGVSB6NR.js +59 -0
- package/dist/node/{chunk-5JQJZADT.js → chunk-ZO2LU4G4.js} +4 -4
- package/dist/node/cli/index.cjs +1126 -211
- package/dist/node/cli/index.js +21 -2
- package/dist/node/{engine-FEN5IYZ5.js → engine-VFHCIEM4.js} +2 -1
- package/dist/node/index.cjs +5623 -2765
- package/dist/node/index.d.cts +1181 -1
- package/dist/node/index.d.ts +1181 -1
- package/dist/node/index.js +1646 -79
- package/dist/node/integrations/svelte.js +4 -3
- package/dist/node/{reverse-W7THPV45.js → reverse-YD3CWIGM.js} +3 -2
- package/dist/node/rules-4DAJ4Z4N.js +7 -0
- package/dist/node/server-FKLVY57V.js +363 -0
- package/dist/node/{validate-EN3M4FUR.js → validate-5PSWJTIC.js} +5 -3
- package/package.json +50 -3
- package/src/__tests__/chronos-project.test.ts +799 -0
- package/src/__tests__/decision-ledger.test.ts +857 -402
- package/src/__tests__/expectations.test.ts +364 -0
- package/src/__tests__/factory.test.ts +426 -0
- package/src/__tests__/mcp-server.test.ts +310 -0
- package/src/__tests__/project.test.ts +396 -0
- package/src/chronos/diff.ts +336 -0
- package/src/chronos/hooks.ts +227 -0
- package/src/chronos/index.ts +83 -0
- package/src/chronos/project-chronicle.ts +198 -0
- package/src/chronos/timeline.ts +152 -0
- package/src/cli/index.ts +28 -0
- package/src/decision-ledger/analyzer-types.ts +280 -0
- package/src/decision-ledger/analyzer.ts +518 -0
- package/src/decision-ledger/contract-verification.ts +456 -0
- package/src/decision-ledger/derivation.ts +158 -0
- package/src/decision-ledger/index.ts +59 -0
- package/src/decision-ledger/report.ts +378 -0
- package/src/decision-ledger/suggestions.ts +287 -0
- package/src/expectations/expectations.ts +471 -0
- package/src/expectations/index.ts +29 -0
- package/src/expectations/types.ts +95 -0
- package/src/factory/factory.ts +634 -0
- package/src/factory/index.ts +27 -0
- package/src/factory/types.ts +64 -0
- package/src/index.browser.ts +83 -0
- package/src/index.ts +134 -0
- package/src/mcp/index.ts +33 -0
- package/src/mcp/server.ts +485 -0
- package/src/mcp/types.ts +161 -0
- package/src/project/index.ts +31 -0
- package/src/project/project.ts +423 -0
- package/src/project/types.ts +87 -0
- package/dist/node/chunk-PTH6MD6P.js +0 -487
- /package/dist/node/{chunk-R2PSBPKQ.js → chunk-TEMFJOIH.js} +0 -0
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RuleResult,
|
|
3
|
+
fact
|
|
4
|
+
} from "./chunk-IG5BJ2MT.js";
|
|
5
|
+
|
|
6
|
+
// src/factory/factory.ts
|
|
7
|
+
var SANITIZE_PATTERNS = {
|
|
8
|
+
"sql-injection": /('|"|;|--|\/\*|\*\/|xp_|exec\s|union\s+select|drop\s+table|insert\s+into|delete\s+from)/i,
|
|
9
|
+
"xss": /(<script|javascript:|on\w+\s*=|<iframe|<object|<embed|<img[^>]+onerror)/i,
|
|
10
|
+
"path-traversal": /(\.\.[/\\]|~\/|\/etc\/|\/proc\/)/i,
|
|
11
|
+
"command-injection": /([;&|`$]|\$\(|>\s*\/)/i
|
|
12
|
+
};
|
|
13
|
+
function inputRules(config = {}) {
|
|
14
|
+
const {
|
|
15
|
+
sanitize = [],
|
|
16
|
+
maxLength = 0,
|
|
17
|
+
required = false,
|
|
18
|
+
fieldName = "input"
|
|
19
|
+
} = config;
|
|
20
|
+
const rules = [];
|
|
21
|
+
const constraints = [];
|
|
22
|
+
if (sanitize.length > 0) {
|
|
23
|
+
rules.push({
|
|
24
|
+
id: `factory/input.sanitize-${fieldName}`,
|
|
25
|
+
description: `Validates ${fieldName} against ${sanitize.join(", ")} patterns`,
|
|
26
|
+
eventTypes: [`${fieldName}.submit`, `${fieldName}.change`],
|
|
27
|
+
contract: {
|
|
28
|
+
ruleId: `factory/input.sanitize-${fieldName}`,
|
|
29
|
+
behavior: `Checks ${fieldName} for dangerous patterns: ${sanitize.join(", ")}`,
|
|
30
|
+
examples: [
|
|
31
|
+
{ given: `${fieldName} contains safe text`, when: "input submitted", then: "input.valid emitted" },
|
|
32
|
+
{ given: `${fieldName} contains <script> tag`, when: "input submitted", then: "input.violation emitted" }
|
|
33
|
+
],
|
|
34
|
+
invariants: [
|
|
35
|
+
`Dangerous ${fieldName} patterns must never pass validation`,
|
|
36
|
+
"All violations must include the violation type"
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
impl: (state, events) => {
|
|
40
|
+
const inputEvent = events.find(
|
|
41
|
+
(e) => e.tag === `${fieldName}.submit` || e.tag === `${fieldName}.change`
|
|
42
|
+
);
|
|
43
|
+
if (!inputEvent) return RuleResult.skip("No input event");
|
|
44
|
+
const value = inputEvent.payload?.value ?? state.context.input?.value ?? "";
|
|
45
|
+
const violations = [];
|
|
46
|
+
for (const type of sanitize) {
|
|
47
|
+
const pattern = SANITIZE_PATTERNS[type];
|
|
48
|
+
if (pattern && pattern.test(value)) {
|
|
49
|
+
violations.push(type);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (violations.length > 0) {
|
|
53
|
+
return RuleResult.emit([
|
|
54
|
+
fact(`${fieldName}.violation`, {
|
|
55
|
+
field: fieldName,
|
|
56
|
+
violations,
|
|
57
|
+
message: `Input failed sanitization: ${violations.join(", ")}`
|
|
58
|
+
})
|
|
59
|
+
]);
|
|
60
|
+
}
|
|
61
|
+
return RuleResult.emit([
|
|
62
|
+
fact(`${fieldName}.valid`, { field: fieldName, sanitized: true })
|
|
63
|
+
]);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (maxLength > 0) {
|
|
68
|
+
constraints.push({
|
|
69
|
+
id: `factory/input.max-length-${fieldName}`,
|
|
70
|
+
description: `${fieldName} must not exceed ${maxLength} characters`,
|
|
71
|
+
contract: {
|
|
72
|
+
ruleId: `factory/input.max-length-${fieldName}`,
|
|
73
|
+
behavior: `Enforces max length of ${maxLength} for ${fieldName}`,
|
|
74
|
+
examples: [
|
|
75
|
+
{ given: `${fieldName} is 10 chars`, when: `maxLength is ${maxLength}`, then: maxLength >= 10 ? "passes" : "violation" }
|
|
76
|
+
],
|
|
77
|
+
invariants: [`${fieldName} length must never exceed ${maxLength}`]
|
|
78
|
+
},
|
|
79
|
+
impl: (state) => {
|
|
80
|
+
const value = state.context.input?.value ?? "";
|
|
81
|
+
if (value.length > maxLength) {
|
|
82
|
+
return `${fieldName} exceeds maximum length of ${maxLength} (got ${value.length})`;
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
if (required) {
|
|
89
|
+
constraints.push({
|
|
90
|
+
id: `factory/input.required-${fieldName}`,
|
|
91
|
+
description: `${fieldName} is required and must not be empty`,
|
|
92
|
+
contract: {
|
|
93
|
+
ruleId: `factory/input.required-${fieldName}`,
|
|
94
|
+
behavior: `Enforces that ${fieldName} is non-empty`,
|
|
95
|
+
examples: [
|
|
96
|
+
{ given: `${fieldName} is "hello"`, when: "checked", then: "passes" },
|
|
97
|
+
{ given: `${fieldName} is empty`, when: "checked", then: "violation" }
|
|
98
|
+
],
|
|
99
|
+
invariants: [`${fieldName} must never be empty when required`]
|
|
100
|
+
},
|
|
101
|
+
impl: (state) => {
|
|
102
|
+
const value = state.context.input?.value ?? "";
|
|
103
|
+
if (value.trim().length === 0) {
|
|
104
|
+
return `${fieldName} is required but empty`;
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return { rules, constraints };
|
|
111
|
+
}
|
|
112
|
+
function toastRules(config = {}) {
|
|
113
|
+
const {
|
|
114
|
+
requireDiff = false,
|
|
115
|
+
autoDismissMs = 0,
|
|
116
|
+
deduplicate = false
|
|
117
|
+
} = config;
|
|
118
|
+
const rules = [];
|
|
119
|
+
const constraints = [];
|
|
120
|
+
rules.push({
|
|
121
|
+
id: "factory/toast.show",
|
|
122
|
+
description: "Emits toast notification with content and config",
|
|
123
|
+
eventTypes: ["toast.request"],
|
|
124
|
+
contract: {
|
|
125
|
+
ruleId: "factory/toast.show",
|
|
126
|
+
behavior: "Shows toast when requested, respecting diff requirement and auto-dismiss",
|
|
127
|
+
examples: [
|
|
128
|
+
{ given: "toast requested with message", when: "toast.request fires", then: "toast.show emitted" },
|
|
129
|
+
...requireDiff ? [{ given: "no diff present", when: "toast.request fires", then: "toast skipped" }] : []
|
|
130
|
+
],
|
|
131
|
+
invariants: [
|
|
132
|
+
"Toast message must be non-empty",
|
|
133
|
+
...requireDiff ? ["Toast must not appear when diff is empty"] : []
|
|
134
|
+
]
|
|
135
|
+
},
|
|
136
|
+
impl: (state, events) => {
|
|
137
|
+
const toastEvent = events.find((e) => e.tag === "toast.request");
|
|
138
|
+
if (!toastEvent) return RuleResult.skip("No toast request");
|
|
139
|
+
const payload = toastEvent.payload;
|
|
140
|
+
const message = payload.message ?? "";
|
|
141
|
+
if (!message) return RuleResult.skip("Empty toast message");
|
|
142
|
+
if (requireDiff) {
|
|
143
|
+
const diff = state.context.diff;
|
|
144
|
+
if (!diff || Object.keys(diff).length === 0) {
|
|
145
|
+
return RuleResult.skip("No diff \u2014 toast suppressed");
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return RuleResult.emit([
|
|
149
|
+
fact("toast.show", {
|
|
150
|
+
message,
|
|
151
|
+
type: payload.type ?? "info",
|
|
152
|
+
autoDismissMs: autoDismissMs > 0 ? autoDismissMs : void 0,
|
|
153
|
+
timestamp: Date.now()
|
|
154
|
+
})
|
|
155
|
+
]);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
if (deduplicate) {
|
|
159
|
+
constraints.push({
|
|
160
|
+
id: "factory/toast.no-duplicates",
|
|
161
|
+
description: "Prevents duplicate toast messages",
|
|
162
|
+
contract: {
|
|
163
|
+
ruleId: "factory/toast.no-duplicates",
|
|
164
|
+
behavior: "Rejects toast if identical message is already showing",
|
|
165
|
+
examples: [
|
|
166
|
+
{ given: "same toast already visible", when: "duplicate toast requested", then: "violation" }
|
|
167
|
+
],
|
|
168
|
+
invariants: ["No two toasts may have the same message simultaneously"]
|
|
169
|
+
},
|
|
170
|
+
impl: (state) => {
|
|
171
|
+
const toasts = state.context.toasts ?? [];
|
|
172
|
+
const messages = toasts.map((t) => t.message);
|
|
173
|
+
const uniqueMessages = new Set(messages);
|
|
174
|
+
if (uniqueMessages.size < messages.length) {
|
|
175
|
+
return "Duplicate toast detected";
|
|
176
|
+
}
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return { rules, constraints };
|
|
182
|
+
}
|
|
183
|
+
function formRules(config = {}) {
|
|
184
|
+
const {
|
|
185
|
+
validateOnBlur = false,
|
|
186
|
+
submitGate = false,
|
|
187
|
+
formName = "form"
|
|
188
|
+
} = config;
|
|
189
|
+
const rules = [];
|
|
190
|
+
const constraints = [];
|
|
191
|
+
if (validateOnBlur) {
|
|
192
|
+
rules.push({
|
|
193
|
+
id: `factory/${formName}.validate-on-blur`,
|
|
194
|
+
description: `Triggers field validation when a ${formName} field loses focus`,
|
|
195
|
+
eventTypes: [`${formName}.blur`],
|
|
196
|
+
contract: {
|
|
197
|
+
ruleId: `factory/${formName}.validate-on-blur`,
|
|
198
|
+
behavior: `Validates the blurred field and emits validation result`,
|
|
199
|
+
examples: [
|
|
200
|
+
{ given: `${formName} field has value`, when: "field loses focus", then: "validation result emitted" }
|
|
201
|
+
],
|
|
202
|
+
invariants: ["Validation must run for every blur event on a registered field"]
|
|
203
|
+
},
|
|
204
|
+
impl: (_state, events) => {
|
|
205
|
+
const blurEvent = events.find((e) => e.tag === `${formName}.blur`);
|
|
206
|
+
if (!blurEvent) return RuleResult.skip("No blur event");
|
|
207
|
+
const payload = blurEvent.payload;
|
|
208
|
+
const field = payload.field ?? "unknown";
|
|
209
|
+
const value = payload.value;
|
|
210
|
+
const valid = value !== null && value !== void 0 && value !== "";
|
|
211
|
+
return RuleResult.emit([
|
|
212
|
+
fact(`${formName}.field-validated`, {
|
|
213
|
+
field,
|
|
214
|
+
valid,
|
|
215
|
+
error: valid ? null : `${field} is required`
|
|
216
|
+
})
|
|
217
|
+
]);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
if (submitGate) {
|
|
222
|
+
constraints.push({
|
|
223
|
+
id: `factory/${formName}.submit-gate`,
|
|
224
|
+
description: `Prevents ${formName} submission when validation has not passed`,
|
|
225
|
+
contract: {
|
|
226
|
+
ruleId: `factory/${formName}.submit-gate`,
|
|
227
|
+
behavior: `Blocks form submission until all fields are valid`,
|
|
228
|
+
examples: [
|
|
229
|
+
{ given: `${formName} is invalid`, when: "submit attempted", then: "violation \u2014 submission blocked" },
|
|
230
|
+
{ given: `${formName} is valid`, when: "submit attempted", then: "passes" }
|
|
231
|
+
],
|
|
232
|
+
invariants: ["Form must not submit while any field has errors"]
|
|
233
|
+
},
|
|
234
|
+
impl: (state) => {
|
|
235
|
+
const form = state.context.form;
|
|
236
|
+
if (!form) return true;
|
|
237
|
+
if (form.submitting && !form.valid) {
|
|
238
|
+
return `${formName} cannot submit: validation has not passed`;
|
|
239
|
+
}
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
rules.push({
|
|
245
|
+
id: `factory/${formName}.dirty-tracking`,
|
|
246
|
+
description: `Tracks whether ${formName} has unsaved changes`,
|
|
247
|
+
eventTypes: [`${formName}.change`, `${formName}.reset`],
|
|
248
|
+
contract: {
|
|
249
|
+
ruleId: `factory/${formName}.dirty-tracking`,
|
|
250
|
+
behavior: "Emits dirty state when form fields change, clears on reset",
|
|
251
|
+
examples: [
|
|
252
|
+
{ given: "field value changed", when: "form.change fires", then: "form.dirty emitted" },
|
|
253
|
+
{ given: "form reset", when: "form.reset fires", then: "form.dirty retracted" }
|
|
254
|
+
],
|
|
255
|
+
invariants: ["Dirty state must reflect actual field changes"]
|
|
256
|
+
},
|
|
257
|
+
impl: (_state, events) => {
|
|
258
|
+
const resetEvent = events.find((e) => e.tag === `${formName}.reset`);
|
|
259
|
+
if (resetEvent) {
|
|
260
|
+
return RuleResult.retract([`${formName}.dirty`], "Form reset");
|
|
261
|
+
}
|
|
262
|
+
const changeEvent = events.find((e) => e.tag === `${formName}.change`);
|
|
263
|
+
if (changeEvent) {
|
|
264
|
+
return RuleResult.emit([
|
|
265
|
+
fact(`${formName}.dirty`, { dirty: true })
|
|
266
|
+
]);
|
|
267
|
+
}
|
|
268
|
+
return RuleResult.skip("No form event");
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
return { rules, constraints };
|
|
272
|
+
}
|
|
273
|
+
function navigationRules(config = {}) {
|
|
274
|
+
const {
|
|
275
|
+
dirtyGuard = false,
|
|
276
|
+
authRequired = false
|
|
277
|
+
} = config;
|
|
278
|
+
const rules = [];
|
|
279
|
+
const constraints = [];
|
|
280
|
+
rules.push({
|
|
281
|
+
id: "factory/navigation.handle",
|
|
282
|
+
description: "Processes navigation requests and emits navigation facts",
|
|
283
|
+
eventTypes: ["navigation.request"],
|
|
284
|
+
contract: {
|
|
285
|
+
ruleId: "factory/navigation.handle",
|
|
286
|
+
behavior: "Emits navigation.allowed or navigation.blocked based on guards",
|
|
287
|
+
examples: [
|
|
288
|
+
{ given: "no guards active", when: "navigation requested", then: "navigation.allowed emitted" },
|
|
289
|
+
...dirtyGuard ? [{ given: "form is dirty", when: "navigation requested", then: "navigation.blocked emitted" }] : [],
|
|
290
|
+
...authRequired ? [{ given: "user not authenticated", when: "navigation requested", then: "navigation.blocked emitted" }] : []
|
|
291
|
+
],
|
|
292
|
+
invariants: [
|
|
293
|
+
"Every navigation request must result in either allowed or blocked",
|
|
294
|
+
...dirtyGuard ? ["Navigation must be blocked when dirty data exists"] : [],
|
|
295
|
+
...authRequired ? ["Navigation must be blocked when not authenticated"] : []
|
|
296
|
+
]
|
|
297
|
+
},
|
|
298
|
+
impl: (state, events) => {
|
|
299
|
+
const navEvent = events.find((e) => e.tag === "navigation.request");
|
|
300
|
+
if (!navEvent) return RuleResult.skip("No navigation request");
|
|
301
|
+
const target = navEvent.payload?.target ?? "/";
|
|
302
|
+
const reasons = [];
|
|
303
|
+
if (dirtyGuard && state.context.dirty) {
|
|
304
|
+
reasons.push("Unsaved changes will be lost");
|
|
305
|
+
}
|
|
306
|
+
if (authRequired && !state.context.authenticated) {
|
|
307
|
+
reasons.push("Authentication required");
|
|
308
|
+
}
|
|
309
|
+
if (reasons.length > 0) {
|
|
310
|
+
return RuleResult.emit([
|
|
311
|
+
fact("navigation.blocked", { target, reasons })
|
|
312
|
+
]);
|
|
313
|
+
}
|
|
314
|
+
return RuleResult.emit([
|
|
315
|
+
fact("navigation.allowed", { target })
|
|
316
|
+
]);
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
if (dirtyGuard) {
|
|
320
|
+
constraints.push({
|
|
321
|
+
id: "factory/navigation.dirty-guard",
|
|
322
|
+
description: "Prevents silent navigation when unsaved changes exist",
|
|
323
|
+
contract: {
|
|
324
|
+
ruleId: "factory/navigation.dirty-guard",
|
|
325
|
+
behavior: "Blocks navigation when dirty state is true",
|
|
326
|
+
examples: [
|
|
327
|
+
{ given: "dirty is true", when: "navigation attempted", then: "violation" },
|
|
328
|
+
{ given: "dirty is false", when: "navigation attempted", then: "passes" }
|
|
329
|
+
],
|
|
330
|
+
invariants: ["Must never silently lose unsaved changes"]
|
|
331
|
+
},
|
|
332
|
+
impl: (state) => {
|
|
333
|
+
if (state.context.dirty && state.facts.some((f) => f.tag === "navigation.allowed")) {
|
|
334
|
+
return "Navigation allowed while dirty \u2014 unsaved changes may be lost";
|
|
335
|
+
}
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
return { rules, constraints };
|
|
341
|
+
}
|
|
342
|
+
function dataRules(config = {}) {
|
|
343
|
+
const {
|
|
344
|
+
optimisticUpdate = false,
|
|
345
|
+
rollbackOnError = false,
|
|
346
|
+
cacheInvalidation = false,
|
|
347
|
+
entityName = "data"
|
|
348
|
+
} = config;
|
|
349
|
+
const rules = [];
|
|
350
|
+
const constraints = [];
|
|
351
|
+
if (optimisticUpdate) {
|
|
352
|
+
rules.push({
|
|
353
|
+
id: `factory/${entityName}.optimistic-update`,
|
|
354
|
+
description: `Applies optimistic update for ${entityName} while request is pending`,
|
|
355
|
+
eventTypes: [`${entityName}.mutate`],
|
|
356
|
+
contract: {
|
|
357
|
+
ruleId: `factory/${entityName}.optimistic-update`,
|
|
358
|
+
behavior: `Immediately emits updated ${entityName} state before server confirmation`,
|
|
359
|
+
examples: [
|
|
360
|
+
{ given: `${entityName} mutation requested`, when: "mutate event fires", then: "optimistic state emitted" }
|
|
361
|
+
],
|
|
362
|
+
invariants: [
|
|
363
|
+
"Optimistic state must store original for rollback",
|
|
364
|
+
"Optimistic update must be distinguishable from confirmed state"
|
|
365
|
+
]
|
|
366
|
+
},
|
|
367
|
+
impl: (_state, events) => {
|
|
368
|
+
const mutateEvent = events.find((e) => e.tag === `${entityName}.mutate`);
|
|
369
|
+
if (!mutateEvent) return RuleResult.skip("No mutation event");
|
|
370
|
+
const payload = mutateEvent.payload;
|
|
371
|
+
return RuleResult.emit([
|
|
372
|
+
fact(`${entityName}.optimistic`, {
|
|
373
|
+
id: payload.id,
|
|
374
|
+
data: payload.data,
|
|
375
|
+
pending: true,
|
|
376
|
+
timestamp: Date.now()
|
|
377
|
+
})
|
|
378
|
+
]);
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
if (rollbackOnError) {
|
|
383
|
+
rules.push({
|
|
384
|
+
id: `factory/${entityName}.rollback`,
|
|
385
|
+
description: `Rolls back optimistic ${entityName} update on error`,
|
|
386
|
+
eventTypes: [`${entityName}.error`],
|
|
387
|
+
contract: {
|
|
388
|
+
ruleId: `factory/${entityName}.rollback`,
|
|
389
|
+
behavior: `Reverts to original ${entityName} state when mutation fails`,
|
|
390
|
+
examples: [
|
|
391
|
+
{ given: "optimistic update was applied", when: "server returns error", then: "rollback emitted, optimistic retracted" }
|
|
392
|
+
],
|
|
393
|
+
invariants: [
|
|
394
|
+
"Rollback must restore original state exactly",
|
|
395
|
+
"Optimistic facts must be retracted on rollback"
|
|
396
|
+
]
|
|
397
|
+
},
|
|
398
|
+
impl: (_state, events) => {
|
|
399
|
+
const errorEvent = events.find((e) => e.tag === `${entityName}.error`);
|
|
400
|
+
if (!errorEvent) return RuleResult.skip("No error event");
|
|
401
|
+
const payload = errorEvent.payload;
|
|
402
|
+
const result = RuleResult.emit([
|
|
403
|
+
fact(`${entityName}.rollback`, {
|
|
404
|
+
id: payload.id,
|
|
405
|
+
error: payload.error,
|
|
406
|
+
timestamp: Date.now()
|
|
407
|
+
})
|
|
408
|
+
]);
|
|
409
|
+
return result;
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
if (cacheInvalidation) {
|
|
414
|
+
rules.push({
|
|
415
|
+
id: `factory/${entityName}.cache-invalidate`,
|
|
416
|
+
description: `Invalidates ${entityName} cache when data changes are confirmed`,
|
|
417
|
+
eventTypes: [`${entityName}.confirmed`, `${entityName}.deleted`],
|
|
418
|
+
contract: {
|
|
419
|
+
ruleId: `factory/${entityName}.cache-invalidate`,
|
|
420
|
+
behavior: `Emits cache invalidation signal when ${entityName} is confirmed or deleted`,
|
|
421
|
+
examples: [
|
|
422
|
+
{ given: `${entityName} mutation confirmed`, when: "confirmed event fires", then: "cache.invalidate emitted" }
|
|
423
|
+
],
|
|
424
|
+
invariants: ["Stale cache entries must be invalidated after confirmed mutations"]
|
|
425
|
+
},
|
|
426
|
+
impl: (_state, events) => {
|
|
427
|
+
const confirmEvent = events.find(
|
|
428
|
+
(e) => e.tag === `${entityName}.confirmed` || e.tag === `${entityName}.deleted`
|
|
429
|
+
);
|
|
430
|
+
if (!confirmEvent) return RuleResult.skip("No confirmation event");
|
|
431
|
+
const payload = confirmEvent.payload;
|
|
432
|
+
return RuleResult.emit([
|
|
433
|
+
fact(`${entityName}.cache-invalidate`, {
|
|
434
|
+
id: payload.id,
|
|
435
|
+
timestamp: Date.now()
|
|
436
|
+
})
|
|
437
|
+
]);
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
constraints.push({
|
|
442
|
+
id: `factory/${entityName}.integrity`,
|
|
443
|
+
description: `Ensures ${entityName} state integrity \u2014 no orphaned optimistic updates`,
|
|
444
|
+
contract: {
|
|
445
|
+
ruleId: `factory/${entityName}.integrity`,
|
|
446
|
+
behavior: "Detects orphaned optimistic updates without pending confirmation",
|
|
447
|
+
examples: [
|
|
448
|
+
{ given: "optimistic update exists without pending request", when: "checked", then: "violation" }
|
|
449
|
+
],
|
|
450
|
+
invariants: [`Every optimistic ${entityName} update must have a corresponding pending request`]
|
|
451
|
+
},
|
|
452
|
+
impl: (state) => {
|
|
453
|
+
const pending = state.context.pending ?? {};
|
|
454
|
+
const optimisticFacts = state.facts.filter((f) => f.tag === `${entityName}.optimistic`);
|
|
455
|
+
for (const optFact of optimisticFacts) {
|
|
456
|
+
const id = optFact.payload?.id;
|
|
457
|
+
if (id && !pending[id]) {
|
|
458
|
+
return `Orphaned optimistic update for ${entityName} id=${id} \u2014 no pending request`;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
return true;
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
return { rules, constraints };
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
export {
|
|
468
|
+
inputRules,
|
|
469
|
+
toastRules,
|
|
470
|
+
formRules,
|
|
471
|
+
navigationRules,
|
|
472
|
+
dataRules
|
|
473
|
+
};
|