@george-veras/acta 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +52 -2
- package/SPEC.md +107 -0
- package/dist/examples/deadlines.d.ts +3 -0
- package/dist/examples/deadlines.js +81 -0
- package/dist/examples/demo.d.ts +1 -0
- package/dist/examples/demo.js +29 -0
- package/dist/examples/effects.d.ts +2 -0
- package/dist/examples/effects.js +155 -0
- package/dist/examples/sketch.d.ts +2 -0
- package/dist/examples/sketch.js +102 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +83 -0
- package/dist/src/config.d.ts +62 -0
- package/dist/src/config.js +57 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.js +5 -0
- package/dist/src/log.d.ts +22 -0
- package/dist/src/log.js +26 -0
- package/dist/src/mcp.d.ts +3 -0
- package/dist/src/mcp.js +124 -0
- package/dist/src/store.d.ts +77 -0
- package/dist/src/store.js +397 -0
- package/dist/src/validate.d.ts +6 -0
- package/dist/src/validate.js +92 -0
- package/package.json +52 -3
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { validate } from "./validate.js";
|
|
3
|
+
import { Log } from "./log.js";
|
|
4
|
+
// Shallow payload matching for deadline satisfaction: every key of
|
|
5
|
+
// the declared subset must be present in the record's payload with a
|
|
6
|
+
// deeply equal value. No subset declared means any commit of the
|
|
7
|
+
// awaited transition satisfies.
|
|
8
|
+
function payloadMatches(payload, subset) {
|
|
9
|
+
if (!subset)
|
|
10
|
+
return true;
|
|
11
|
+
if (typeof payload !== "object" || payload === null)
|
|
12
|
+
return false;
|
|
13
|
+
const record = payload;
|
|
14
|
+
return Object.entries(subset).every(([key, value]) => JSON.stringify(record[key]) === JSON.stringify(value));
|
|
15
|
+
}
|
|
16
|
+
// A pathological configuration can cascade effects without bound (each
|
|
17
|
+
// effect emitting a transition that retriggers itself at a new
|
|
18
|
+
// version, each version a fresh idempotency key). The engine caps the
|
|
19
|
+
// runs in one unbroken cascade; anything past the cap is recorded as
|
|
20
|
+
// failed with the cap as its reason, never silently dropped.
|
|
21
|
+
const MAX_CASCADE_RUNS = 64;
|
|
22
|
+
// The Acta core (SPEC S1) plus the effects engine (SPEC S3): one
|
|
23
|
+
// store, divided into slices; every write a named transition,
|
|
24
|
+
// serialized through a single commit queue, appended to an append-only
|
|
25
|
+
// log, and assigned the next version. After each commit, the effects
|
|
26
|
+
// subscribed to the changed slice run, once per version. There is no
|
|
27
|
+
// other write path: state changes only inside commit(), and effects
|
|
28
|
+
// write only by proposing transitions through that same path.
|
|
29
|
+
export class Acta {
|
|
30
|
+
config;
|
|
31
|
+
state;
|
|
32
|
+
version;
|
|
33
|
+
seq;
|
|
34
|
+
log;
|
|
35
|
+
now;
|
|
36
|
+
queue;
|
|
37
|
+
fxQueue;
|
|
38
|
+
fxBook;
|
|
39
|
+
fxPath;
|
|
40
|
+
fxCascade;
|
|
41
|
+
fxPending;
|
|
42
|
+
dlBook;
|
|
43
|
+
dlPath;
|
|
44
|
+
constructor(config, options = {}) {
|
|
45
|
+
this.config = config;
|
|
46
|
+
this.state = {};
|
|
47
|
+
for (const [name, decl] of Object.entries(config.slices)) {
|
|
48
|
+
const check = validate(decl.initial, decl.schema, `slice "${name}" initial`);
|
|
49
|
+
if (!check.ok) {
|
|
50
|
+
throw new Error(`invalid initial value: ${check.errors.join("; ")}`);
|
|
51
|
+
}
|
|
52
|
+
this.state[name] = structuredClone(decl.initial);
|
|
53
|
+
}
|
|
54
|
+
this.version = 0;
|
|
55
|
+
this.seq = 0;
|
|
56
|
+
this.log = options.logPath ? new Log(options.logPath) : null;
|
|
57
|
+
this.now = options.now ?? (() => new Date());
|
|
58
|
+
this.queue = Promise.resolve();
|
|
59
|
+
this.fxQueue = Promise.resolve();
|
|
60
|
+
this.fxCascade = 0;
|
|
61
|
+
this.fxPending = 0;
|
|
62
|
+
this.fxPath =
|
|
63
|
+
options.effectsPath ?? (options.logPath ? options.logPath + ".effects.json" : null);
|
|
64
|
+
this.fxBook = new Map();
|
|
65
|
+
if (this.fxPath && existsSync(this.fxPath)) {
|
|
66
|
+
const parsed = JSON.parse(readFileSync(this.fxPath, "utf8"));
|
|
67
|
+
for (const [key, value] of Object.entries(parsed))
|
|
68
|
+
this.fxBook.set(key, value);
|
|
69
|
+
}
|
|
70
|
+
this.dlPath =
|
|
71
|
+
options.deadlinesPath ?? (options.logPath ? options.logPath + ".deadlines.json" : null);
|
|
72
|
+
this.dlBook = new Map();
|
|
73
|
+
if (this.dlPath && existsSync(this.dlPath)) {
|
|
74
|
+
const parsed = JSON.parse(readFileSync(this.dlPath, "utf8"));
|
|
75
|
+
for (const [key, value] of Object.entries(parsed))
|
|
76
|
+
this.dlBook.set(key, value);
|
|
77
|
+
}
|
|
78
|
+
if (this.log) {
|
|
79
|
+
const existing = this.log.readAll();
|
|
80
|
+
if (existing.length > 0) {
|
|
81
|
+
const replayed = Acta.replay(config, existing);
|
|
82
|
+
this.state = replayed.state;
|
|
83
|
+
this.version = replayed.version;
|
|
84
|
+
this.seq = existing.length;
|
|
85
|
+
// At-least-once recovery: versions committed before a crash
|
|
86
|
+
// whose effects never ran (or were never recorded) are due
|
|
87
|
+
// again at boot. The bookkeeping is what makes "again" safe.
|
|
88
|
+
for (const record of existing)
|
|
89
|
+
this.scheduleEffects(record);
|
|
90
|
+
// Deadline satisfaction is derived from the log, so a
|
|
91
|
+
// deadline satisfied before a restart is still satisfied
|
|
92
|
+
// after it, with no bookkeeping to trust.
|
|
93
|
+
for (const record of existing)
|
|
94
|
+
this.markSatisfied(record);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
get currentVersion() {
|
|
99
|
+
return this.version;
|
|
100
|
+
}
|
|
101
|
+
// A read is stamped with the version it reflects. An agent never asks
|
|
102
|
+
// another agent how things are; it reads the slice.
|
|
103
|
+
read(slice) {
|
|
104
|
+
if (!(slice in this.state))
|
|
105
|
+
throw new Error(`unknown slice "${slice}"`);
|
|
106
|
+
return { value: structuredClone(this.state[slice]), version: this.version };
|
|
107
|
+
}
|
|
108
|
+
// Proposing a transition enqueues it on the single commit queue.
|
|
109
|
+
// Callers may propose concurrently; commits are serialized in the
|
|
110
|
+
// order the proposals reached the queue, each with its own version.
|
|
111
|
+
propose(seat, transition, payload, opts = {}) {
|
|
112
|
+
const run = () => this.commit(seat, transition, payload, opts.baseVersion, opts.token);
|
|
113
|
+
const result = this.queue.then(run, run);
|
|
114
|
+
this.queue = result.then(() => undefined, () => undefined);
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
// Resolves when every effect due for the versions committed so far
|
|
118
|
+
// has run, including cascades those effects caused.
|
|
119
|
+
async settle() {
|
|
120
|
+
let tail;
|
|
121
|
+
do {
|
|
122
|
+
tail = this.fxQueue;
|
|
123
|
+
await tail;
|
|
124
|
+
} while (tail !== this.fxQueue);
|
|
125
|
+
}
|
|
126
|
+
commit(seat, transitionName, payload, baseVersion, token) {
|
|
127
|
+
const decl = this.config.transitions[transitionName];
|
|
128
|
+
if (!decl)
|
|
129
|
+
return { ok: false, reason: `unknown transition "${transitionName}"` };
|
|
130
|
+
if (!(seat in this.config.seats)) {
|
|
131
|
+
return { ok: false, reason: `unknown seat "${seat}"` };
|
|
132
|
+
}
|
|
133
|
+
const seatDecl = this.config.seats[seat];
|
|
134
|
+
if (seatDecl.token !== undefined && token !== seatDecl.token) {
|
|
135
|
+
return { ok: false, reason: `seat "${seat}" requires its token` };
|
|
136
|
+
}
|
|
137
|
+
if (!decl.seats.includes(seat)) {
|
|
138
|
+
return {
|
|
139
|
+
ok: false,
|
|
140
|
+
reason: `seat "${seat}" may not emit transition "${transitionName}"`,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
if (seatDecl.manifest &&
|
|
144
|
+
!seatDecl.manifest.transitions.includes(transitionName)) {
|
|
145
|
+
return {
|
|
146
|
+
ok: false,
|
|
147
|
+
reason: `seat "${seat}" manifest does not include transition "${transitionName}"`,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
if (baseVersion !== undefined && baseVersion !== this.version) {
|
|
151
|
+
return {
|
|
152
|
+
ok: false,
|
|
153
|
+
reason: `baseVersion ${baseVersion} does not match current version ${this.version}`,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
const inputCheck = validate(payload, decl.input, "payload");
|
|
157
|
+
if (!inputCheck.ok) {
|
|
158
|
+
return { ok: false, reason: `invalid payload: ${inputCheck.errors.join("; ")}` };
|
|
159
|
+
}
|
|
160
|
+
const current = this.state[decl.slice];
|
|
161
|
+
if (decl.guard) {
|
|
162
|
+
const verdict = decl.guard(structuredClone(current), structuredClone(payload));
|
|
163
|
+
if (verdict !== true)
|
|
164
|
+
return { ok: false, reason: `guard refused: ${verdict}` };
|
|
165
|
+
}
|
|
166
|
+
const next = decl.apply(structuredClone(current), structuredClone(payload));
|
|
167
|
+
const sliceCheck = validate(next, this.config.slices[decl.slice].schema, `slice "${decl.slice}"`);
|
|
168
|
+
if (!sliceCheck.ok) {
|
|
169
|
+
return {
|
|
170
|
+
ok: false,
|
|
171
|
+
reason: `transition "${transitionName}" produced an invalid slice: ${sliceCheck.errors.join("; ")}`,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
const record = {
|
|
175
|
+
seq: this.seq + 1,
|
|
176
|
+
version: this.version + 1,
|
|
177
|
+
at: this.now().toISOString(),
|
|
178
|
+
seat,
|
|
179
|
+
transition: transitionName,
|
|
180
|
+
slice: decl.slice,
|
|
181
|
+
payload: structuredClone(payload),
|
|
182
|
+
baseVersion: this.version,
|
|
183
|
+
};
|
|
184
|
+
this.state[decl.slice] = next;
|
|
185
|
+
this.version = record.version;
|
|
186
|
+
this.seq = record.seq;
|
|
187
|
+
if (this.log)
|
|
188
|
+
this.log.append(record);
|
|
189
|
+
this.markSatisfied(record);
|
|
190
|
+
this.scheduleEffects(record);
|
|
191
|
+
return { ok: true, version: this.version, record };
|
|
192
|
+
}
|
|
193
|
+
// Register a deadline against the current version. Refusals come
|
|
194
|
+
// back as data, like proposal refusals: a duplicate name, an
|
|
195
|
+
// undeclared transition, a seat the expiry transition does not
|
|
196
|
+
// allow, or a due time that does not parse.
|
|
197
|
+
registerDeadline(decl) {
|
|
198
|
+
if (this.dlBook.has(decl.name)) {
|
|
199
|
+
return { ok: false, reason: `deadline "${decl.name}" is already registered` };
|
|
200
|
+
}
|
|
201
|
+
if (!(decl.awaiting in this.config.transitions)) {
|
|
202
|
+
return { ok: false, reason: `unknown transition "${decl.awaiting}"` };
|
|
203
|
+
}
|
|
204
|
+
const expiry = this.config.transitions[decl.expiryTransition];
|
|
205
|
+
if (!expiry) {
|
|
206
|
+
return { ok: false, reason: `unknown transition "${decl.expiryTransition}"` };
|
|
207
|
+
}
|
|
208
|
+
if (!(decl.seat in this.config.seats)) {
|
|
209
|
+
return { ok: false, reason: `unknown seat "${decl.seat}"` };
|
|
210
|
+
}
|
|
211
|
+
if (!expiry.seats.includes(decl.seat)) {
|
|
212
|
+
return {
|
|
213
|
+
ok: false,
|
|
214
|
+
reason: `seat "${decl.seat}" may not emit transition "${decl.expiryTransition}"`,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
if (Number.isNaN(Date.parse(decl.dueAt))) {
|
|
218
|
+
return { ok: false, reason: `deadline "${decl.name}" has an unparseable dueAt` };
|
|
219
|
+
}
|
|
220
|
+
this.dlBook.set(decl.name, {
|
|
221
|
+
...decl,
|
|
222
|
+
registeredAtVersion: this.version,
|
|
223
|
+
status: "open",
|
|
224
|
+
});
|
|
225
|
+
this.persistDeadlines();
|
|
226
|
+
return { ok: true };
|
|
227
|
+
}
|
|
228
|
+
deadlineStatus(name) {
|
|
229
|
+
return this.dlBook.get(name)?.status;
|
|
230
|
+
}
|
|
231
|
+
// The server's one clock act, and it is deliberately explicit:
|
|
232
|
+
// embedders call this on their own cadence (the MCP server, a
|
|
233
|
+
// timer, a test with an injected clock), and every commit path can
|
|
234
|
+
// rely on satisfaction already being marked. Each open deadline
|
|
235
|
+
// whose due time has passed writes its expiry transition by itself,
|
|
236
|
+
// through the commit queue; a refused expiry stays open and the
|
|
237
|
+
// refusal is returned, so the next check retries it in the open.
|
|
238
|
+
async checkDeadlines() {
|
|
239
|
+
const nowMs = this.now().getTime();
|
|
240
|
+
const outcomes = [];
|
|
241
|
+
for (const deadline of this.dlBook.values()) {
|
|
242
|
+
if (deadline.status !== "open")
|
|
243
|
+
continue;
|
|
244
|
+
if (Date.parse(deadline.dueAt) > nowMs)
|
|
245
|
+
continue;
|
|
246
|
+
const result = await this.propose(deadline.seat, deadline.expiryTransition, deadline.payload ?? { deadline: deadline.name });
|
|
247
|
+
outcomes.push({ name: deadline.name, result });
|
|
248
|
+
if (result.ok) {
|
|
249
|
+
deadline.status = "expired";
|
|
250
|
+
deadline.resolvedAtVersion = result.version;
|
|
251
|
+
this.persistDeadlines();
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return outcomes;
|
|
255
|
+
}
|
|
256
|
+
markSatisfied(record) {
|
|
257
|
+
let changed = false;
|
|
258
|
+
for (const deadline of this.dlBook.values()) {
|
|
259
|
+
if (deadline.status === "open" &&
|
|
260
|
+
deadline.awaiting === record.transition &&
|
|
261
|
+
record.version > deadline.registeredAtVersion &&
|
|
262
|
+
payloadMatches(record.payload, deadline.matchPayload)) {
|
|
263
|
+
deadline.status = "satisfied";
|
|
264
|
+
deadline.resolvedAtVersion = record.version;
|
|
265
|
+
changed = true;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
if (changed)
|
|
269
|
+
this.persistDeadlines();
|
|
270
|
+
}
|
|
271
|
+
persistDeadlines() {
|
|
272
|
+
if (!this.dlPath)
|
|
273
|
+
return;
|
|
274
|
+
const all = {};
|
|
275
|
+
for (const [k, v] of this.dlBook)
|
|
276
|
+
all[k] = v;
|
|
277
|
+
writeFileSync(this.dlPath, JSON.stringify(all, null, 2) + "\n", "utf8");
|
|
278
|
+
}
|
|
279
|
+
dueEffects(record) {
|
|
280
|
+
return (this.config.effects ?? []).filter((effect) => {
|
|
281
|
+
if (effect.reads !== record.slice)
|
|
282
|
+
return false;
|
|
283
|
+
if (effect.after && !effect.after.includes(record.transition))
|
|
284
|
+
return false;
|
|
285
|
+
return !this.fxBook.has(`${effect.name}@${record.version}`);
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
scheduleEffects(record) {
|
|
289
|
+
for (const effect of this.dueEffects(record)) {
|
|
290
|
+
this.fxPending += 1;
|
|
291
|
+
const run = () => this.runEffect(effect, record);
|
|
292
|
+
this.fxQueue = this.fxQueue.then(run, run);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
async runEffect(effect, record) {
|
|
296
|
+
try {
|
|
297
|
+
await this.runEffectBody(effect, record);
|
|
298
|
+
}
|
|
299
|
+
finally {
|
|
300
|
+
this.fxPending -= 1;
|
|
301
|
+
if (this.fxPending === 0)
|
|
302
|
+
this.fxCascade = 0;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
async runEffectBody(effect, record) {
|
|
306
|
+
const key = `${effect.name}@${record.version}`;
|
|
307
|
+
if (this.fxBook.has(key))
|
|
308
|
+
return;
|
|
309
|
+
this.fxCascade += 1;
|
|
310
|
+
if (this.fxCascade > MAX_CASCADE_RUNS) {
|
|
311
|
+
this.recordEffect(key, {
|
|
312
|
+
status: "failed",
|
|
313
|
+
at: this.now().toISOString(),
|
|
314
|
+
error: `cascade limit of ${MAX_CASCADE_RUNS} effect runs exceeded`,
|
|
315
|
+
});
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
try {
|
|
319
|
+
await effect.run({
|
|
320
|
+
version: record.version,
|
|
321
|
+
record: {
|
|
322
|
+
seat: record.seat,
|
|
323
|
+
transition: record.transition,
|
|
324
|
+
slice: record.slice,
|
|
325
|
+
payload: structuredClone(record.payload),
|
|
326
|
+
},
|
|
327
|
+
read: (slice) => this.read(slice),
|
|
328
|
+
emit: async (transition, payload) => {
|
|
329
|
+
const outcome = await this.propose(effect.seat, transition, payload);
|
|
330
|
+
return outcome.ok
|
|
331
|
+
? { ok: true, version: outcome.version }
|
|
332
|
+
: { ok: false, reason: outcome.reason };
|
|
333
|
+
},
|
|
334
|
+
});
|
|
335
|
+
this.recordEffect(key, { status: "done", at: this.now().toISOString() });
|
|
336
|
+
}
|
|
337
|
+
catch (caught) {
|
|
338
|
+
const message = caught instanceof Error ? caught.message : String(caught);
|
|
339
|
+
this.recordEffect(key, { status: "failed", at: this.now().toISOString(), error: message });
|
|
340
|
+
if (effect.failureTransition) {
|
|
341
|
+
// The failure enters the state as a transition, through the
|
|
342
|
+
// same commit path as everything else. If even that commit is
|
|
343
|
+
// refused, the bookkeeping above is the record of the failure.
|
|
344
|
+
await this.propose(effect.seat, effect.failureTransition, {
|
|
345
|
+
effect: effect.name,
|
|
346
|
+
version: record.version,
|
|
347
|
+
error: message,
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
recordEffect(key, entry) {
|
|
353
|
+
this.fxBook.set(key, entry);
|
|
354
|
+
if (this.fxPath) {
|
|
355
|
+
const all = {};
|
|
356
|
+
for (const [k, v] of this.fxBook)
|
|
357
|
+
all[k] = v;
|
|
358
|
+
writeFileSync(this.fxPath, JSON.stringify(all, null, 2) + "\n", "utf8");
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
// The recorded outcome of one (effect, version) pair, if it ran.
|
|
362
|
+
effectOutcome(effectName, version) {
|
|
363
|
+
return this.fxBook.get(`${effectName}@${version}`);
|
|
364
|
+
}
|
|
365
|
+
snapshot() {
|
|
366
|
+
return { version: this.version, state: structuredClone(this.state) };
|
|
367
|
+
}
|
|
368
|
+
// Replay folds the log through the transitions' apply functions and
|
|
369
|
+
// reproduces the state at any version, from the initial values or
|
|
370
|
+
// from a snapshot plus the tail of the log after it.
|
|
371
|
+
static replay(config, records, opts = {}) {
|
|
372
|
+
let state = {};
|
|
373
|
+
let version = 0;
|
|
374
|
+
if (opts.fromSnapshot) {
|
|
375
|
+
state = structuredClone(opts.fromSnapshot.state);
|
|
376
|
+
version = opts.fromSnapshot.version;
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
for (const [name, decl] of Object.entries(config.slices)) {
|
|
380
|
+
state[name] = structuredClone(decl.initial);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
const toVersion = opts.toVersion ?? Number.POSITIVE_INFINITY;
|
|
384
|
+
for (const record of records) {
|
|
385
|
+
if (record.version <= version)
|
|
386
|
+
continue;
|
|
387
|
+
if (record.version > toVersion)
|
|
388
|
+
break;
|
|
389
|
+
const decl = config.transitions[record.transition];
|
|
390
|
+
if (!decl)
|
|
391
|
+
throw new Error(`log names unknown transition "${record.transition}"`);
|
|
392
|
+
state[record.slice] = decl.apply(structuredClone(state[record.slice]), structuredClone(record.payload));
|
|
393
|
+
version = record.version;
|
|
394
|
+
}
|
|
395
|
+
return { state, version };
|
|
396
|
+
}
|
|
397
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// Minimal JSON Schema subset validator, zero dependencies.
|
|
2
|
+
//
|
|
3
|
+
// Supported keywords: type, enum, required, properties,
|
|
4
|
+
// additionalProperties (boolean), items, minItems, maxItems,
|
|
5
|
+
// minimum, maximum, minLength, maxLength. Anything outside the subset
|
|
6
|
+
// is ignored, and the subset is documented rather than implied: Acta
|
|
7
|
+
// v0 validates the shapes a coordination store actually carries.
|
|
8
|
+
function typeOf(value) {
|
|
9
|
+
if (value === null)
|
|
10
|
+
return "null";
|
|
11
|
+
if (Array.isArray(value))
|
|
12
|
+
return "array";
|
|
13
|
+
if (typeof value === "number" && Number.isInteger(value))
|
|
14
|
+
return "integer";
|
|
15
|
+
return typeof value;
|
|
16
|
+
}
|
|
17
|
+
function matchesType(value, type) {
|
|
18
|
+
const actual = typeOf(value);
|
|
19
|
+
if (type === "number")
|
|
20
|
+
return actual === "number" || actual === "integer";
|
|
21
|
+
return actual === type;
|
|
22
|
+
}
|
|
23
|
+
export function validate(value, schema, path = "$") {
|
|
24
|
+
const errors = [];
|
|
25
|
+
if (schema.enum !== undefined) {
|
|
26
|
+
const options = schema.enum;
|
|
27
|
+
if (!options.some((option) => JSON.stringify(option) === JSON.stringify(value))) {
|
|
28
|
+
errors.push(`${path}: expected one of ${JSON.stringify(options)}`);
|
|
29
|
+
return { ok: false, errors };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (schema.type !== undefined) {
|
|
33
|
+
const types = Array.isArray(schema.type) ? schema.type : [schema.type];
|
|
34
|
+
if (!types.some((type) => matchesType(value, type))) {
|
|
35
|
+
errors.push(`${path}: expected type ${types.join("|")}, got ${typeOf(value)}`);
|
|
36
|
+
return { ok: false, errors };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (typeof value === "number") {
|
|
40
|
+
if (typeof schema.minimum === "number" && value < schema.minimum) {
|
|
41
|
+
errors.push(`${path}: ${value} is below minimum ${schema.minimum}`);
|
|
42
|
+
}
|
|
43
|
+
if (typeof schema.maximum === "number" && value > schema.maximum) {
|
|
44
|
+
errors.push(`${path}: ${value} is above maximum ${schema.maximum}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (typeof value === "string") {
|
|
48
|
+
if (typeof schema.minLength === "number" && value.length < schema.minLength) {
|
|
49
|
+
errors.push(`${path}: shorter than minLength ${schema.minLength}`);
|
|
50
|
+
}
|
|
51
|
+
if (typeof schema.maxLength === "number" && value.length > schema.maxLength) {
|
|
52
|
+
errors.push(`${path}: longer than maxLength ${schema.maxLength}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (Array.isArray(value)) {
|
|
56
|
+
if (typeof schema.minItems === "number" && value.length < schema.minItems) {
|
|
57
|
+
errors.push(`${path}: fewer than minItems ${schema.minItems}`);
|
|
58
|
+
}
|
|
59
|
+
if (typeof schema.maxItems === "number" && value.length > schema.maxItems) {
|
|
60
|
+
errors.push(`${path}: more than maxItems ${schema.maxItems}`);
|
|
61
|
+
}
|
|
62
|
+
if (schema.items !== undefined) {
|
|
63
|
+
value.forEach((item, index) => {
|
|
64
|
+
const sub = validate(item, schema.items, `${path}[${index}]`);
|
|
65
|
+
errors.push(...sub.errors);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
70
|
+
const record = value;
|
|
71
|
+
if (Array.isArray(schema.required)) {
|
|
72
|
+
for (const key of schema.required) {
|
|
73
|
+
if (!(key in record))
|
|
74
|
+
errors.push(`${path}: missing required property "${key}"`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const properties = (schema.properties ?? {});
|
|
78
|
+
for (const [key, subSchema] of Object.entries(properties)) {
|
|
79
|
+
if (key in record) {
|
|
80
|
+
const sub = validate(record[key], subSchema, `${path}.${key}`);
|
|
81
|
+
errors.push(...sub.errors);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (schema.additionalProperties === false) {
|
|
85
|
+
for (const key of Object.keys(record)) {
|
|
86
|
+
if (!(key in properties))
|
|
87
|
+
errors.push(`${path}: unexpected property "${key}"`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return { ok: errors.length === 0, errors };
|
|
92
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
|
+
"author": "George V\u00e9ras Valentim",
|
|
3
|
+
"bin": {
|
|
4
|
+
"acta": "dist/src/cli.js"
|
|
5
|
+
},
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/george-veras/acta/issues"
|
|
8
|
+
},
|
|
9
|
+
"description": "Acta: coordination as a function of the state, when the components interpret. One store, named transitions, effects subscribed to slices.",
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=22.18"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/src/index.d.ts",
|
|
16
|
+
"default": "./dist/src/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"SPEC.md"
|
|
22
|
+
],
|
|
23
|
+
"homepage": "https://acta.works",
|
|
24
|
+
"keywords": [
|
|
25
|
+
"mcp",
|
|
26
|
+
"model-context-protocol",
|
|
27
|
+
"multi-agent",
|
|
28
|
+
"coordination",
|
|
29
|
+
"state-management",
|
|
30
|
+
"llm-agents",
|
|
31
|
+
"ai-agents"
|
|
32
|
+
],
|
|
33
|
+
"license": "MIT",
|
|
2
34
|
"name": "@george-veras/acta",
|
|
3
|
-
"
|
|
4
|
-
|
|
5
|
-
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+https://github.com/george-veras/acta.git"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"demo": "node examples/sketch.ts",
|
|
44
|
+
"test": "node --test test/s1.test.ts test/s2.test.ts test/s3.test.ts test/s4.test.ts",
|
|
45
|
+
"build": "tsc -p tsconfig.json",
|
|
46
|
+
"prepack": "npm run build"
|
|
47
|
+
},
|
|
48
|
+
"type": "module",
|
|
49
|
+
"version": "0.1.1",
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/node": "^22.20.7",
|
|
52
|
+
"typescript": "^5.9.3"
|
|
53
|
+
},
|
|
54
|
+
"types": "./dist/src/index.d.ts"
|
|
6
55
|
}
|